home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gprof / gpfsrc09.zoo / gprof.c < prev    next >
C/C++ Source or Header  |  1993-03-02  |  87KB  |  3,047 lines

  1. /* `gprof', analyze gmon.out and print a profile.
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* GNU gprof was written mainly by Jay Fenlason.  */
  19.  
  20. #include <assert.h>
  21. #include "getopt.h"
  22. #ifdef __STDC__
  23. #include <stddef.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26.  
  27. #ifdef __GNUC__
  28. #define alloca __builtin_alloca
  29. #else /* not __GNUC__ */
  30. #ifdef sparc
  31. #include <alloca.h>
  32. #endif
  33. #endif /* not __GNUC__ */
  34.  
  35. #else /* no __STDC__ */
  36.  
  37. #ifdef sparc
  38. #include <alloca.h>
  39. #endif /* sparc */
  40. #include <varargs.h>
  41. #define const    /* */
  42. typedef unsigned int size_t;
  43. #endif /* no __STDC__ */
  44.  
  45. #undef NULL
  46. #include <stdio.h>
  47. #undef NULL
  48. #define NULL 0
  49.  
  50. #ifdef atarist
  51. #    include <st-out.h>
  52. #else
  53. #  if !defined(A_OUT) && !defined(MACH_O)
  54. #    define A_OUT
  55. #  endif
  56.  
  57. #  ifdef A_OUT
  58. #    ifdef COFF_ENCAPSULATE
  59. #      include "a.out.encap.h"
  60. #    else
  61. #     include <a.out.h>
  62. #    endif
  63. #  endif
  64.  
  65. #  ifdef MACH_O
  66. #    ifndef A_OUT
  67. #      include <nlist.h>
  68. #      ifndef N_TEXT
  69. #        define N_TEXT 0x04
  70. #      endif
  71. #    endif
  72. #    include <sys/loader.h>
  73. #  endif
  74. #endif
  75.  
  76. #ifdef HAVE_SETITIMER
  77. #include <sys/time.h>
  78. #endif
  79.  
  80. #include "gmon.h"
  81. /* #include <nlist.h> */
  82.  
  83. #ifdef USG
  84. #define index strchr
  85. #define bzero(s, n) (memset((s), 0, (n)))
  86. #define bcopy(from, to, n) (memcpy ((to), (from), (n)))
  87. #endif
  88.  
  89. /* Special macros designed to remove some __STDC__ ugliness from my source
  90.    files.  Instead, I use these (which may be just as ugly).  Instead of using
  91. extern foo(); 
  92.    or
  93. extern foo(int,double);
  94.    I use
  95. extern foo FUN2(int, double);
  96.    which is expanded to the right thing depending on whether you're using an
  97.    ANSI cc or not.
  98.  
  99.    Also, instead of saying
  100. type
  101. foo(x,y)
  102. int x;
  103. double y;
  104.    or
  105. type
  106. foo(int x, double y)
  107.    I use
  108. type
  109. foo FUN2(int, x, double, y)
  110. Which is also expanded into the right thing. . .
  111.  */
  112.  
  113. #ifdef __STDC__
  114. #define var_start(x,y) va_start(x,y)
  115.  
  116. /* These macros expand into ANSI prototypes */
  117. #define FUN0()        (void)
  118. #define EXT0()        (void)
  119.  
  120. #define FUN1(t1,a1)    (t1 a1)
  121. #define EXT1(t1)    (t1)
  122. #define FUN1N(t1,a1)    (t1 a1, ...)
  123. #define EXT1N(t1)    (t1, ...)
  124.  
  125. #define FUN2(t1,a1,t2,a2)    (t1 a1,t2 a2)
  126. #define EXT2(t1, t2)        (t1, t2)
  127. #define FUN2N(t1,a1,t2,a2)    (t1 a1,t2 a2, ...)
  128. #define EXT2N(t1, t2)        (t1, t2, ...)
  129.  
  130. #define FUN3(t1,a1,t2,a2,t3,a3)    (t1 a1, t2 a2, t3 a3)
  131. #define EXT3(t1, t2, t3)    (t1, t2, t3)
  132. #define FUN3N(t1,a1,t2,a2,t3,a3)(t1 a1, t2 a2, t3 a3, ...)
  133. #define EXT3N(t1, t2, t3)    (t1, t2, t3, ...)
  134.  
  135. #define FUN4(t1,a1,t2,a2,t3,a3,t4,a4)    (t1 a1, t2 a2, t3 a3, t4 a4)
  136. #define EXT4(t1, t2, t3, t4)        (t1, t2, t3, t4)
  137.  
  138. #define FUN5(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)
  139. #define EXT5(t1, t2, t3, t4, t5)        (t1, t2, t3, t4, t5)
  140.  
  141. #define FUN6(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6)
  142. #define EXT6(t1, t2, t3, t4, t5, t6)            (t1, t2, t3, t4, t5, t6)
  143.  
  144. #define FUN7(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7)
  145. #define EXT7(t1, t2, t3, t4, t5, t6, t7)        (t1, t2, t3, t4, t5, t6, t7)
  146.  
  147. #define FUN8(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8)
  148. #define EXT8(t1, t2, t3, t4, t5, t6, t7, t8)            (t1, t2, t3, t4, t5, t6, t7, t8)
  149.  
  150. #define FUN9(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9)
  151. #define EXT9(t1, t2, t3, t4, t5, t6, t7, t8, t9)            (t1, t2, t3, t4, t5, t6, t7, t8, t9)
  152.  
  153. #define FUN10(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9,t10,a10)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9, t10 a10)
  154. #define EXT10(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)                (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)
  155.  
  156. #define FUN11(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9,t10,a10,t11,a11)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9, t10 a10, t11 a11)
  157. #define EXT11(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11)                (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11)
  158.  
  159. #define FUN12(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9,t10,a10,t11,a11,t12,a12)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9, t10 a10, t11 a11, t12 a12)
  160. #define EXT12(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12)                (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12)
  161.  
  162. #define FUN13(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9,t10,a10,t11,a11,t12,a12,t13,a13)    (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9, t10 a10, t11 a11, t12 a12, t13 a13)
  163. #define EXT13(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13)                (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13)
  164.  
  165.  
  166. #else
  167. /* Non-ANSI */
  168. #define var_start(x,y) va_start(x)
  169.  
  170. /* These macros expand into old-style function definitions */
  171.  
  172. #define FUN0()    ()
  173. #define EXT0()    ()
  174.  
  175. #define FUN1(t1,a1)    (a1) t1 a1;
  176. #define EXT1(t1)    ()
  177. #define FUN1N(t1,a1)    (a1,va_alist) t1 a1; va_dcl
  178. #define EXT1N(t1)    ()
  179.  
  180. #define FUN2(t1,a1,t2,a2)    (a1, a2) t1 a1; t2 a2;
  181. #define EXT2(t1, t2)        ()
  182. #define FUN2N(t1,a1,t2,a2,va_alist) (a1, a2) t1 a1; t2 a2; va_dcl
  183. #define EXT2N(t1, t2)        ()
  184.  
  185. #define FUN3(t1,a1,t2,a2,t3,a3) (a1, a2, a3) t1 a1; t2 a2; t3 a3;
  186. #define EXT3(t1, t2, t3)    ()
  187. #define FUN3N(t1,a1,t2,a2,t3,a3) (a1, a2, a3, va_alist) t1 a1; t2 a2; t3 a3; va_dcl
  188. #define EXT3N(t1, t2, t3)    ()
  189.  
  190. #define FUN4(t1,a1,t2,a2,t3,a3,t4,a4)    (a1, a2, a3, a4) t1 a1; t2 a2; t3 a3; t4 a4;
  191. #define EXT4(t1, t2, t3, t4)        ()
  192.  
  193. #define FUN5(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5) (a1, a2, a3, a4, a5) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5;
  194. #define EXT5(t1, t2, t3, t4, t5)    ()
  195.  
  196. #define FUN6(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6) (a1, a2, a3, a4, a5, a6) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6;
  197. #define EXT6(t1, t2, t3, t4, t5, t6)    ()
  198.  
  199. #define FUN7(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7) (a1, a2, a3, a4, a5, a6, a7) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6; t7 a7;
  200. #define EXT7(t1, t2, t3, t4, t5, t6, t7)    ()
  201.  
  202. #define FUN8(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8) (a1, a2, a3, a4, a5, a6, a7, a8) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6; t7 a7; t8 a8;
  203. #define EXT8(t1, t2, t3, t4, t5, t6, t7, t8)    ()
  204.  
  205. #define FUN9(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9) (a1, a2, a3, a4, a5, a6, a7, a8, a9) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6; t7 a7; t8 a8; t9 a9;
  206. #define EXT9(t1, t2, t3, t4, t5, t6, t7, t8, t9)    ()
  207.  
  208. #define FUN10(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9,t10,a10) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6; t7 a7; t8 a8; t9 a9; t10 a10;
  209. #define EXT10(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)    ()
  210.  
  211. #define FUN11(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9,t10,a10,t11,a11) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6; t7 a7; t8 a8; t9 a9; t10 a10; t11 a11;
  212. #define EXT11(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11)    ()
  213.  
  214. #define FUN12(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9,t10,a10,t11,a11,t12,a12) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6; t7 a7; t8 a8; t9 a9; t10 a10; t11 a11; t12 a12;
  215. #define EXT12(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12)    ()
  216.  
  217. #define FUN13(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7,t8,a8,t9,a9,t10,a10,t11,a11,t12,a12,t13,a13) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6; t7 a7; t8 a8; t9 a9; t10 a10; t11 a11; t12 a12; t13 a13;
  218. #define EXT13(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13)    ()
  219.  
  220.  
  221. #endif
  222.  
  223. #ifdef atarist
  224. #  ifdef VPRINTF_MISSING
  225. #    undef VPRINTF_MISSING
  226. #  endif
  227. #endif
  228.  
  229. #ifdef VPRINTF_MISSING
  230. /* The following will work for some systems.  */
  231. #define vfprintf(stream, format, ap) _doprnt (format, ap, stream)
  232. int
  233. vsprintf FUN3(char *, into, char *, s, va_list, ap)
  234. {
  235.     int ret;
  236.     FILE f;
  237.  
  238.     f._cnt = 32767;
  239.     /* taking address and dereferencing deals with the fact that
  240.        f._ptr can be either a char * or an unsigned char *.  */
  241.     *(char **)&f._ptr = into;
  242.     f._flag = _IOWRT+_IOSTRG;
  243.     ret = _doprnt(s, ap, &f);
  244.     *f._ptr = 0;
  245.     return (ret);
  246. }
  247. #endif
  248.  
  249. /* Names or default names for various files.  */
  250. #define OBJ_NAM "a.out"
  251. #define MON_NAM "gmon.out"
  252. #define SUM_NAM "gmon.sum"
  253.  
  254. /* Debugging stuff */
  255.  
  256. #define DEBUG
  257.  
  258. /* A mask of flags defined below.  */
  259. long debug=0;
  260.  
  261. /* Print obnoxious msgs about the a.out file, and the amusing values therein */
  262. #define DB_AFILE    (1<<0)    /* a.out file */
  263.  
  264. /* Describe in detail the rending and tearing of the gmon.out file */
  265. #define DB_GFILE    (1<<1)    /* gmon.out file */
  266.  
  267. /* Describe in detail the writing out of the gmon.sum file */
  268. #define DB_SUM        (1<<2)    /* gmon.sum file */
  269.  
  270. /* Print neeto messages as we deal with -e -E -f and -F options */
  271. #define DB_OPT        (1<<3)    /* Options */
  272.  
  273. /* Print msgs whenever the ring buffer is used */
  274. /* This probably doesn't work since the ring buffer code was
  275.    moved into the utilities file */
  276. #define DB_RB        (1<<4)    /* Ring buffer */
  277.  
  278. /* Print msgs about cycles */
  279. #define DB_CYC        (1<<5)
  280.  
  281. /* Print msgs about assigning the histogram entries to functions */
  282. #define DB_HISTO        (1<<6)
  283.  
  284. /* Print obnoxious msgs here and there as we do our stuff */
  285. #define DB_MISC        (1<<31)    /* misc stuff */
  286.  
  287. #ifdef DEBUG
  288. #define PRINT_OBNOXIOUS_DEBUG_MESSAGE(x, msg) if (debug&x) dbgprintf msg
  289. #else
  290. #define PRINT_OBNOXIOUS_DEBUG_MESSAGE(x, msg)
  291. #endif
  292.  
  293. /* LessThan EQual GreaterThan */
  294. /* These get returned by the various comparison functions */
  295. #define LT (-1)
  296. #define EQ (0)
  297. #define GT (1)
  298.  
  299. /* Note since *ALL* non-zero values are true, do *NOT* say
  300.       if (x==TRUE)
  301.    These values are here only for saying
  302.       return TRUE;
  303.    and things like that.   */
  304.  
  305. #define TRUE (1)
  306. #define FALSE (0)
  307.  
  308. /* The floating point datatype to use for storing
  309.    propagated info.  Float should be big enough */
  310. #define FLOAT    double
  311.  
  312. /* Used when creating a gmon.sum file */
  313. /* #define FUDGE_FACTOR (sizeof (CHUNK)) */
  314. #define FUDGE_FACTOR    0
  315.  
  316. /* Description of one symbol in gprof's internal symbol table.
  317.    There is one of these for each function and one for each cycle.
  318.  
  319.    NAME is the name of the function or cycle.
  320.    VALUE is the address of the start of the function.
  321.  
  322.    CALLS and CALLED are chains of edges for calls into and out of
  323.    this function.  These constitute the call graph.
  324.  
  325.    NCALLS is the total number
  326.    of times this function called other functions,
  327.    NCALLED is the total number of times this function was called.
  328.    Note that if NCALLED is zero, but NCALLS is nozero, this function
  329.    must have been started magically.  It happens.  (Signals, etc.)
  330.  
  331.    RECURSIVE is the total number of times this function was called
  332.    recursively.
  333.  
  334.    HISTO is the total number of histogram counts for this function.
  335.  
  336.    SUB_HISTO is the total number of histogram counts for this function
  337.    plus time propagated from the children.
  338.  
  339.    CYCNUM is the number of the cycle this function is in,
  340.    or the number of this cycle is this entry is for a cycle.
  341.    -1 means the function isn't in any cycles.  0 means the
  342.    cycle-ness of the function hasn't been determined yet.
  343.  
  344.    NUMINDEX is the index number assigned to this function
  345.    for the call graph.
  346.  
  347.    If FLAG is non-zero, this function is in the process of being
  348.    saved from the oblivion caused by the -f or -F options.  */
  349.  
  350. struct mesym {
  351.     char *name;
  352.     unsigned long value;
  353.  
  354.     struct symlist *calls;
  355.     unsigned ncalls;
  356.     struct symlist *called;
  357.     unsigned ncalled;
  358.  
  359.     unsigned recursive;
  360.  
  361.     unsigned histo;
  362.     FLOAT sub_histo;
  363.     int    cycnum;
  364.     int    numindex;
  365.     int    flag;
  366. };
  367.  
  368. /* Vector of `struct mesym' for all functions,
  369.    sorted in ascending order by VALUE field for binary search.  */
  370. struct mesym *syms;
  371. int nsym=0;            /* Length of vector */
  372.  
  373. /* Vector of `struct mesym' for all cycles so far identified.  */
  374. struct mesym **cycles;
  375. int number_of_cycles = 0;        /* Length of vector */
  376.  
  377. /* Structure for an edges of the call graph.
  378.  
  379.    Each edge--each pair of functions A and B such that A called B--
  380.    is represented by one of these structures.  It appears in
  381.    A's `calls' chain and in B's `called' chain.
  382.  
  383.    Meanwhile, the structure describes its meaning with the
  384.    SYM_FROM and SYM_TO fields, which point to the symbol entries
  385.    for A and B.
  386.  
  387.    Basically, if you got here from a mesym's calls
  388.    pointer, SYM_FROM points back to that symbol, NEXT_FROM is irrevelant,
  389.    SYM_TO points to the symbol it called, and NEXT_TO points to the
  390.    next one in the list.
  391.  
  392.    If you got here from a mesym's called pointer, SYM_FROM points to
  393.    the symbol that called it, NEXT_FROM points to the next one in the
  394.    list, SYM_TO points back to the symbol, and NEXT_TO is irrelevant.
  395.  
  396.    NCALLS is the number of times SYM_FROM called SYM_TO,
  397.    PROP_TIME is the amount of time in SYM_TO itself propagated to SYM_FROM,
  398.    CHILD_TIME is the amount of time
  399.      propagated from SYM_TO's children to SYM_FROM.  */
  400.    
  401. struct symlist {
  402.     struct mesym *sym_from;
  403.     struct symlist *next_from;
  404.     struct mesym *sym_to;
  405.     struct symlist *next_to;
  406.  
  407.     unsigned ncalls;
  408.     FLOAT    prop_time;
  409.     FLOAT    child_time;
  410. };
  411.  
  412. /* argv[0], here for the sake of error messages.  */
  413. char *myname = 0;
  414.  
  415. /* Header of the executable file.  */
  416. #ifdef atarist
  417. struct aexec exec_header;
  418. #else
  419. struct exec exec_header;
  420. #endif
  421.  
  422. /* Name of the executable file.  */
  423. char *exec_file_name;
  424.  
  425. /* The string table of the executable file.
  426.    Each symbol entry in the file contains an index in the string table.
  427.    When the symbols are in core, we relocate them to point to their names,
  428.    which remain inside the string table.  */
  429. char *strs;
  430.  
  431. #ifdef atarist
  432. /* The maximal length of an symbol name.
  433.    This is initially eight, but will be changed to 22 in the code
  434.    if any GST compatible long symbols are detected in the symbol table.
  435.    NOTE: do not change it manually here */
  436. int max_atari_sym_length = 8;
  437. #endif
  438.  
  439. /* Header of the first gmon.out file we read.  This is used to (try to)
  440.    make sure all the rest of the gmon.out files agree with the first one
  441.    (and the executable.)  */
  442. struct gm_header hdr;
  443.  
  444. /* Number of clock ticks per second.  Read from the kernel's memory,
  445.    this number tells us how long an interval a single stab in the
  446.    histogram represents. */
  447. long ticks;
  448.  
  449. /* This is an array of pointers to symbols.  These are the functions that
  450.    we'll have to print out in the flat graph.  */
  451. struct mesym **flat_profile_functions;
  452. int number_of_flat_profile_functions =  0; /* Size of vector */
  453.  
  454. /* This is an array of pointers to symbols.  These are the functions that
  455.    we should mention in the call tree.  */
  456. struct mesym **functions_in_call_tree;
  457. int number_of_functions_in_call_tree= 0;
  458. struct mesym **f_end;
  459.  
  460. /* Number of slots in the histogram.  */
  461. int nhist;
  462.  
  463. /* Total number of counts in the histogram.  */
  464. unsigned long tothist = 0;
  465.  
  466. /* Pointer to the histogram itself.  */
  467. unsigned CHUNK *histo;    /* Stabs */
  468.  
  469. /* Now that I've re-written the ring_buffer code, we need this */
  470. void *ring_buffer;
  471.  
  472. /* Record the specified options.  */
  473.  
  474. /* If the -a option is given, static (private) functions are not read into
  475.    the symbol table.  This means that time spent in them, calls to them, etc,
  476.    are instead added to whatever function was loaded next to it in the a.out
  477.    file.  This is compatable with UN*X gprof.  (Bleh.)  The right thing to do
  478.    is keep track of time spent in static functions, and forget about it,
  479.    instead of adding it to another hapless function.  Rms disagrees with me.
  480.    I think its evil to add histogram time to a function simply because it
  481.    happend to be loaded in memory just before a function we don't want to print. */
  482. int no_locals = 0;        /* -a flag */
  483.  
  484. /* The -b option tells gprof to not print out the obnoxious blurbs telling
  485.    what all the fields of the output mean.  This is useful if you've seen
  486.    the blurbs a gzillion times and you only want to look at your numbers. */
  487. int no_blurbs = 0;        /* -b flag */
  488.  
  489. /* If the -s option is given, gprof will write out a 'sum file' gmon.sum
  490.    which is a gmon.out file that contains the profile info from all the
  491.    gmon.out files that gprof read in. */
  492. int write_sum_file = 0;        /* -s option */
  493.  
  494. /* The -z option tells gprof to include functions with zero usage (never called,
  495.    and used no time) in the output.  Usually, such functions are considered
  496.    boring, and aren't printed. */
  497. int print_zeros = 0;        /* -z option */
  498.  
  499. /* The -v option tells gprof to caugh up its version and Patchlevel */
  500. int version = 0;        /* -v option */
  501.  
  502. /* The -e option takes a function name, and suppress the printing of that
  503.    function and its descendents from the call graph profile.  (If its
  504.    descendents are called from elswhere, well. . .)  (Currently, they're
  505.    printed, and the -e'd function is shown under the list of parents so
  506.    you can see where the child's time is disappearing to. . .)
  507.  
  508.    The -E option takes a function name, and not only -e's it, but
  509.    removes the time spent in the function from the total time.  (Its as if
  510.    that function never existed (unless it calls something that is also
  511.    called from somewhere else, in which case. . .  (Currently it works
  512.    as described for -e above.))
  513.  
  514.    -f prints only the call tree for its argument.
  515.  
  516.    -F is like -f, but it uses only the time in the function and its
  517.    descendents for computing total time.
  518.  */
  519.  
  520. enum option_type { SMALL_E, BIG_E, SMALL_F, BIG_F,
  521.            /* Like BIG_E except don't print a warning if the
  522.               function doesn't exist.  */
  523.            REMOVE_TIME_IF_THERE };
  524.  
  525. struct filter {
  526.     char *name;
  527.     enum option_type type;
  528. };
  529.  
  530. /* Vector with an element for each -e, -E, -f or -F option specified.  */
  531. struct filter *filters;
  532. /* Length of the vector.  */
  533. int nfilters;
  534.  
  535. /* These values are stored in the flag field of a struct mesym so we can know
  536.    what we're doing to that function */
  537.  
  538. /* This one is being saved by a -f or -F flag */
  539. #define SAVE_ME        01
  540. /* This one is being killed by a -e or -E flag */
  541. #define KILL_ME        02
  542.  
  543. /* Blurbs that are copied verbatim into the output file
  544.    to explain the data in the tables.
  545.  
  546.    If your compiler can't stand this, split this up into a vector
  547.    of strings, and print them one after another. */
  548.  
  549. char *first_blurb = "\n\
  550.  The above table shows how much time was spent directly in each\n\
  551.  function.  The table was sorted by the amount of time the computer\n\
  552.  spent in each function.\n\n\
  553.  % time        This is the percentage of the total execution time\n\
  554.         the program spent in this function.  These should all add\n\
  555.         up to 100%.\n\n\
  556.  seconds       This is the total number of seconds the computer spent\n\
  557.         executing this function.\n\n\
  558.  cumsec        This is the cumulative total number of seconds the\n\
  559.         computer spent executing this functions, plus the time spent\n\
  560.      in all the functions above this one in this table.\n\n\
  561.  calls         This is the total number of times the function was called.\n\
  562.         If there isn't a number here, the function wasn't compiled with\n\
  563.     the profiler enabled, and further information about this function\n\
  564.     is limited.  In particular, all information about where this \n\
  565.     function was called from has been lost.\n\n\
  566.  function      This is the name of the function.\n\
  567. \f";
  568.  
  569. char *second_blurb = "\n\
  570.  This table describes the call tree of the program, and was sorted by\n\
  571.  the total amount of time spent in each function and its children.\n\n\
  572.  Each entry in this table consists of several lines.  The line with the\n\
  573.  index number at the left hand margin lists the current function.\n\
  574.  The lines above it list the functions that called this function,\n\
  575.  and the lines below it list the functions this one called.\n\
  576.  This line lists:\n\
  577.      index    A unique number given to each element of the table.\n\
  578.         Index numbers are sorted numerically.\n\
  579.         The index number is printed next to every function name so\n\
  580.         it is easier to look up where the function in the table.\n\n\
  581.      % time    This is the percentage of the `total' time that was spent\n\
  582.         in this function and its children.  Note that due to\n\
  583.         different viewpoints, functions excluded by options, etc,\n\
  584.         these numbers will NOT add up to 100%.\n\n\
  585.      self    This is the total amount of time spent in this function.\n\n\
  586.      children    This is the total amount of time propagated into this\n\
  587.         function by its children.\n\n\
  588.      called    This is the number of times the function was called.\n\
  589.         If the function called itself recursively, the number\n\
  590.         only includes non-recursive calls, and is followed by\n\
  591.         a `+' and the number of recursive calls.\n\n\
  592.      name    The name of the current function.  The index number is\n\
  593.         printed after it.  If the function is a member of a\n\
  594.         cycle, the cycle number is printed between the\n\
  595.         function's name and the index number.\n\n\n\
  596.  For the function's parents, the fields have the following meanings:\n\n\
  597.      self    This is the amount of time that was propagated directly\n\
  598.         from the function into this parent.\n\n\
  599.      children    This is the amount of time that was propagated from\n\
  600.         the function's children into this parent.\n\n\
  601.      called    This is the number of times this parent called the\n\
  602.         function `/' the total number of times the function\n\
  603.         was called.  Recursive calls to the function are not\n\
  604.         included in the number after the `/'.\n\n\
  605.      name    This is the name of the parent.  The parent's index\n\
  606.         number is printed after it.  If the parent is a\n\
  607.         member of a cycle, the cycle number is printed between\n\
  608.         the name and the index number.\n\n\
  609.  If the parents of the function cannot be determined, the word\n\
  610.  `<spontaneous>' is printed in the `name' field, and all the other\n\
  611.  fields are blank.\n\n\
  612.  For the function's children, the fields have the following meanings:\n\n\
  613.      self    This is the amount of time that was propagated directly\n\
  614.         from the child into the function.\n\n\
  615.      children    This is the amount of time that was propagated from the\n\
  616.         child's children to the function.\n\n\
  617.      called    This is the number of times the function called\n\
  618.         this child `/' the total number of times the child\n\
  619.         was called.  Recursive calls by the child are not\n\
  620.         listed in the number after the `/'.\n\n\
  621.      name    This is the name of the child.  The child's index\n\
  622.         number is printed after it.  If the child is a\n\
  623.         member of a cycle, the cycle number is printed\n\
  624.         between the name and the index number.\n\n\
  625.  If there are any cycles (circles) in the call graph, there is an\n\
  626.  entry for the cycle-as-a-whole.  This entry shows who called the\n\
  627.  cycle (as parents) and the members of the cycle (as children.)\n\
  628.  The `+' recursive calls entry shows the number of function calls that\n\
  629.  were internal to the cycle, and the calls entry for each member shows,\n\
  630.  for that member, how many times it was called from other members of\n\
  631.  the cycle.\n\n";
  632.  
  633.  
  634. /* Prototypes for all the functions.  */
  635.  
  636. int    main EXT2(int, char **);
  637. #ifndef atarist
  638. int    read_header_info EXT6 (char *, FILE *, long int *, unsigned int *,
  639.                    long int *, unsigned int *);
  640. #endif
  641. void    print_flat_profile EXT0();
  642. void    print_call_graph EXT0();
  643. void    write_summary EXT0();
  644. void    filter_graph EXT0();
  645. FLOAT    convert_and_round EXT1(FLOAT);
  646. void    add_to_lists EXT3(struct mesym *, struct mesym *, unsigned);
  647. void    delete_from_lists EXT2(struct mesym*, struct mesym *);
  648. void    flushfuns EXT0();
  649. void    findcycles EXT0();
  650. void    kill_children EXT2(struct mesym *, int);
  651. void    save_the_children EXT2(struct mesym *, int);
  652. void    remove_from_call_tree EXT1(struct mesym **);
  653. struct mesym **find_funp_from_name EXT1(char *);
  654. struct mesym **find_funp_from_pointer EXT1(struct mesym *);
  655. void    read_syms EXT2(FILE *, int);
  656. struct mesym *val_to_sym EXT1(unsigned long);
  657. #ifndef atarist
  658. int    badsym EXT1(struct nlist *);
  659. #else
  660. int    badsym EXT1(struct asym *);
  661. #endif
  662. int    symcmp EXT2(const void *, const void *);
  663. int    timecmp EXT2(const void *, const void *);
  664. int    callcmp EXT2(const void *, const void *);
  665. int    treetimecmp EXT2(const void *, const void *);
  666. int    listcmp EXT2(const void *, const void *);
  667. void    readgm EXT1(char *);
  668. void    print_blurb EXT1(char *blurb);
  669. long    get_ticks EXT0();
  670. void    add_filter EXT2(char *name, int flag);
  671. void    print_sorted_list EXT3(int, int, struct symlist *);
  672.  
  673. void    fatal EXT1N(char *);
  674. void    fatal_io EXT2(char *, char *);
  675.  
  676. FILE *ck_fopen EXT2(char *, char *);
  677. void ck_fseek EXT3(FILE *, long, int);
  678. void ck_fread EXT4(void *, size_t, size_t, FILE *);
  679. void ck_fwrite EXT4(void *, size_t, size_t, FILE *);
  680. void ck_fclose EXT1(FILE *);
  681.  
  682. void *ck_malloc EXT1(size_t);
  683. void *ck_calloc EXT2(size_t, size_t);
  684. void *ck_realloc EXT2(void *, size_t);
  685.  
  686. char *mk_sprintf EXT1N(char *);
  687.  
  688. void *init_ring_buffer EXT0();
  689. void push_ring_buffer EXT2(void *, void *);
  690. void *pop_ring_buffer EXT1(void *);
  691. int ring_buffer_isnt_empty EXT1(void *);
  692. void flush_ring_buffer EXT1(void *);
  693. void print_version EXT0();
  694.  
  695. /* C++ demangler stuff.  */
  696. char *cplus_demangle EXT1(char *);
  697. void fprint_name EXT2(FILE *, char *);
  698.  
  699. void fatal EXT1N(char *);
  700.  
  701. void *malloc EXT1(size_t);
  702. void *realloc EXT2(void *,size_t);
  703. void free EXT1(void *);
  704.  
  705.  
  706. /* Since we don't have prototypes for the system funs, we add them
  707.    ourselves. . . */
  708. int    atoi EXT1(const char *);
  709. long    atol EXT1(const char *);
  710. double    atof EXT1(const char *);
  711.  
  712. #ifndef NeXT /* NeXT has a bug in their include files. */
  713. void    qsort EXT4(void *, size_t, size_t, int (*)(const void *, const void *));
  714. #endif
  715.  
  716. void    exit EXT1(int);
  717.  
  718. int    strcmp EXT2(const char *, const char *);
  719. char    *index EXT2(const char *, int);
  720. int    printf EXT1N(const char *);
  721. int    fprintf EXT2N(FILE *, const char *);
  722. /* char    *sprintf EXT2N(const char *, const char *); */
  723.  
  724. int    puts EXT1(const char *);
  725. int    fputs EXT2(const char *, FILE *);
  726.  
  727. int    fputc EXT2(int, FILE *);
  728. #ifdef __STDC__
  729. size_t    fread EXT4(void *, size_t, size_t, FILE *);
  730. #else
  731. int    fread EXT4(void *, size_t, size_t, FILE *);
  732. #endif
  733.  
  734. #ifndef atarist
  735. int    nlist EXT2(const char *, struct nlist *);
  736. #endif
  737.  
  738. #ifndef VPRINTF_MISSING
  739. int    vfprintf EXT3(FILE *, const char *, va_list);
  740. #endif
  741.  
  742. void dbgprintf EXT1N(char *);
  743. void dumpsyms EXT0();
  744. void dumpfuns EXT0();
  745.  
  746. /* And now, the program.  */
  747.  
  748. int
  749. main FUN2(int, ac, char **, av)
  750. {
  751.   FILE    *fp;
  752.   int    argc;
  753.   char    **argv;
  754.   int    c;
  755.   int n;
  756.  
  757.   extern char *optarg;
  758.   extern int optind, opterr;
  759.  
  760. #ifndef __STDC__
  761.     static
  762. #endif
  763.     struct option long_options[] = 
  764.     {
  765.       {"no-static",     0, &no_locals,          1},
  766.       {"brief",     0, &no_blurbs,          1},
  767.       {"no-prof",     1, 0,            'e'},
  768.       {"no-time",     1, 0,             'E'},
  769.       {"only-prof",     1, 0,                 'f'},
  770.       {"only-time",     1, 0,                 'F'},
  771.       {"sum",        0, &write_sum_file,   1},
  772.       {"zeros",        0, &print_zeros,      1},
  773.       {"version",    0, 0,               'v'},
  774.       {NULL, 0, NULL, 0}
  775.     };
  776.      
  777.   char *name = '\0';
  778.   int    ind;  
  779. #ifndef atarist
  780.   long int syms_offset;
  781.   unsigned int syms_size;
  782.   long int strs_offset;
  783.   unsigned int strs_size;
  784. #endif
  785.  
  786. #ifdef atarist
  787.   _malloczero(1);
  788. #endif
  789.   argc=ac;
  790.   argv=av;
  791.  
  792.   myname= argv[0];
  793.  
  794.   /* Omit profiling internal functions from call graph.  */
  795. #ifdef sparc
  796.  
  797. #else
  798.   add_filter ("mcount", BIG_E);
  799. #endif
  800.   /* add_filter ("mcleanup", BIG_E); Seems to have dissappeared? */
  801.  
  802.   /* GCC output doesn't have this function.  */
  803.   add_filter ("moncontrol", REMOVE_TIME_IF_THERE);
  804.  
  805.   ring_buffer=init_ring_buffer ();
  806.  
  807.     while ((c = getopt_long (argc, argv, "abcdD:e:E:f:F:svz", long_options,
  808.                  &ind)) !=EOF) {
  809.     if (c == 0 && long_options[ind].flag == 0)
  810.       c = long_options[ind].val;
  811.     switch (c) {
  812.     case  0 :
  813.       break;
  814.     case 'a':
  815.       no_locals= TRUE;
  816.       break;
  817.     case 'b':
  818.       no_blurbs = TRUE;
  819.       break;
  820.     case 'c':
  821.       fatal ("The -c option is not supported");
  822.     case 'd':
  823.       debug= -1;
  824.       break;
  825.     case 'D':
  826.       debug=atoi (optarg);
  827.       break;
  828.     case 's':
  829.       write_sum_file= TRUE;
  830.       break;
  831.     case 'z':
  832.       print_zeros = TRUE;
  833.       break;
  834.     case 'e':
  835.       add_filter (optarg, SMALL_E);
  836.       break;
  837.     case 'E':
  838.       add_filter (optarg, BIG_E);
  839.       break;
  840.     case 'f':
  841.       add_filter (optarg, SMALL_F);
  842.       break;
  843.     case 'F':
  844.       add_filter (optarg, BIG_F);
  845.       break;
  846.     case 'v':
  847.       version = 1;
  848.       break;
  849.     default:
  850.       print_version();
  851.       fprintf (stderr, "\
  852. Usage: %s [-absvz] [-e func] [-E func] [-f func] [-F func]\n\
  853.        [+no-static] [+brief] [+no-prof func]\n\
  854.        [+no-time func] [+only-prof func]\n\
  855.        [+only-time func] [+sum] [+version] [+zeros] executable gmon.out...\n",
  856.              myname);
  857.       exit(1);
  858.     }
  859.   }
  860.   if (optind<argc) {
  861.     exec_file_name=argv[optind];
  862.     optind++;
  863.   }
  864.   if(version) {
  865.     print_version();
  866.   }
  867.   if (!exec_file_name)
  868.     exec_file_name=OBJ_NAM;
  869.  
  870.   /* Open the a.out file, and read in selected portions */
  871. #ifdef atarist
  872.   fp=ck_fopen (exec_file_name, "rb");
  873.   ck_fread ((void *)&exec_header, sizeof (exec_header), 1, fp);
  874.  
  875.   /* Make sure its really an a.out file.  If it isn't yell and scream
  876.      and stamp our feet. */
  877.   if (A_BADMAG (exec_header))
  878.     fatal ("`%s' is not an executable file", exec_file_name);
  879.  
  880.   if(exec_header.a_syms == 0L)
  881.     fatal ("`%s' has no symbols", exec_file_name);
  882.  
  883.   /* Read the symbols, and put the interesting ones (sorted) in SYMS.  */
  884.   /* read_syms builds strs too */
  885.   ck_fseek (fp, A_SYMOFF (exec_header), 0);
  886.   read_syms (fp, exec_header.a_syms/sizeof (struct asym));
  887.  
  888. #else /* ! atarist */
  889.  
  890.   /* Open the a.out file, and read in selected portions */
  891.   fp=ck_fopen (exec_file_name, "r");
  892.  
  893.   /* Make sure its really an a.out file.  If it isn't yell and scream
  894.      and stamp our feet. */
  895.   if (!read_header_info(exec_file_name, fp, &syms_offset, &syms_size, &strs_offset, &strs_size))
  896.     fatal ("`%s' is not an executable file", exec_file_name);
  897.  
  898.   /* Read in the string table.  */
  899.   ck_fseek (fp, strs_offset + sizeof strs_size, 0);
  900.   strs=(char *)ck_malloc (strs_size);
  901.   ck_fread ((void *)(strs+sizeof (long)), sizeof (char), strs_size-sizeof (long), fp);
  902.  
  903.   /* Read the symbols, and put the interesting ones (sorted) in SYMS.  */
  904.   ck_fseek (fp, syms_offset, 0);
  905.   read_syms (fp, syms_size / sizeof (struct nlist));
  906. #endif /* atarist */
  907.  
  908.   ck_fclose (fp);
  909.   /* We are done with the a.out file */
  910.  
  911.   /* Read in the gmon.out files; accumulate all histogram data in HISTO
  912.      and put all number-of-calls figures into the call graph.  */
  913.   if (optind>=argc) readgm (MON_NAM);
  914.   else {
  915.     while (optind<argc) {
  916.       readgm (argv[optind]);
  917.       optind++;
  918.     }
  919.   }
  920.  
  921.   /* If a summary output file is wanted,
  922.      we can do it straightaway since we have merged the data.  */
  923.  
  924.   if (write_sum_file) {
  925.     write_summary ();
  926.     exit (0);
  927.   }
  928.  
  929.   /* Find out how many clock ticks/second our machine puts out */
  930.   ticks=get_ticks ();
  931.  
  932.   /* Assign the ticks in the histogram to the functions they represent */
  933.   {
  934.     int    n;
  935.     unsigned long pos;
  936.     struct mesym *sym;
  937.  
  938. #ifdef atarist
  939. /* NOTE: The 2 should track HIST_SCALE in lib/gmon.c */
  940.     int incr = (hdr.high - hdr.low) / nhist;
  941.     if ((hdr.high - hdr.low) != (2 * nhist) ) 
  942. #else
  943.     int incr = (hdr.high - hdr.low) / (nhist - 1);
  944.     if ((hdr.high - hdr.low) != (4 * (nhist -1)) ) 
  945. #endif
  946.       exit (91);
  947.  
  948. #ifndef atarist    /* on the St we dont relocate at 0 */
  949.     for (n=0, pos=hdr.low, sym= &syms[0]; n<nhist; n++, pos+=incr) {
  950. #else
  951.     for (n=0, pos=0, sym= &syms[0]; n<nhist; n++, pos+=incr) {
  952. #endif
  953.       if (histo[n]) {
  954.  
  955.     /* We've found the right symbol when *sym<=pos && *(sym+1)>pos */
  956.     /* This means POS lies between sym and the one after it */
  957.  
  958.     while ((sym+1)->value<=pos)
  959.       sym++;
  960.     if ((sym+1)->value<(pos+incr))
  961.       {
  962.         /* Wow!  On the edge.  Split the time between the symbols */
  963.         sym->histo+=(histo[n]+1)/2;
  964.         (sym+1)->histo+=(histo[n])/2;
  965.         PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_HISTO, ("%05lx %02d-> %s (%d) %s (%d)\n", pos, histo[n], sym->name, sym->histo, (sym+1)->name, (sym+1)->histo));
  966.       } else {
  967.         sym->histo+=histo[n];
  968.         PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_HISTO, ("%05lx %02d-> %s (%d)\n", pos, histo[n], sym->name, sym->histo));
  969.       }
  970.       }
  971.     }
  972.   }
  973.  
  974.   /* Avoid division by zero if there wasn't any time collected!  */
  975.   if (tothist==0)
  976.     tothist=1;
  977.  
  978.   print_flat_profile ();
  979.  
  980.   print_call_graph ();
  981.  
  982.   if (debug&DB_AFILE)
  983.     dumpsyms ();
  984.  
  985.   if (debug&DB_AFILE)
  986.     dumpfuns ();
  987.   return(0);
  988. }
  989.  
  990. #ifndef atarist
  991. /* Read various information from the header of an object file.
  992.    Return 0 for failure or 1 for success.  */
  993.  
  994. int
  995. read_header_info (name, fp, syms_offset, syms_size, strs_offset, strs_size)
  996.      char *name;
  997.      FILE *fp;
  998.      long int *syms_offset;
  999.      unsigned int *syms_size;
  1000.      long int *strs_offset;
  1001.      unsigned int *strs_size;
  1002. {
  1003. #ifdef A_OUT
  1004.   {
  1005.     struct exec hdr;
  1006.  
  1007.     ck_fseek (fp, 0L, 0);
  1008. #ifdef HEADER_SEEK
  1009.     HEADER_SEEK (fp);
  1010. #endif
  1011.  
  1012.     if (fread ((char *) &hdr, sizeof hdr, 1, fp) == 1 && !N_BADMAG(hdr))
  1013.       {
  1014.     *syms_offset = N_SYMOFF (hdr);
  1015.     *syms_size = hdr.a_syms;
  1016.     *strs_offset = N_STROFF (hdr);
  1017.     ck_fseek (fp, N_STROFF (hdr), 0);
  1018.     if (fread ((char *) strs_size, sizeof *strs_size, 1, fp) != 1)
  1019.       fatal ("error reading string table of `%s'", name);
  1020.     return 1;
  1021.       }
  1022.   }
  1023. #endif
  1024.  
  1025. #ifdef MACH_O
  1026.   {
  1027.     struct mach_header mach_header;
  1028.     struct load_command *load_command;
  1029.     struct symtab_command *symtab_command;
  1030.     char *hdrbuf;
  1031.     int cmd, symtab_seen;
  1032.  
  1033.     ck_fseek (fp, 0L, 0);
  1034.     if (fread ((char *) &mach_header, sizeof mach_header, 1, fp) == 1
  1035.     && mach_header.magic == MH_MAGIC)
  1036.       {
  1037.     hdrbuf = ck_malloc (mach_header.sizeofcmds);
  1038.     if (fread (hdrbuf, mach_header.sizeofcmds, 1, fp) != 1)
  1039.       fatal ("failure reading load commands of file `%s'", name);
  1040.     load_command = (struct load_command *) hdrbuf;
  1041.     symtab_seen = 0;
  1042.     for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
  1043.       {
  1044.         if (load_command->cmd == LC_SYMTAB)
  1045.           {
  1046.         symtab_seen = 1;
  1047.         symtab_command = (struct symtab_command *) load_command;
  1048.         *syms_offset = symtab_command->symoff;
  1049.         *syms_size = symtab_command->nsyms * sizeof (struct nlist);
  1050.         *strs_offset = symtab_command->stroff;
  1051.         *strs_size = symtab_command->strsize;
  1052.           }
  1053.         load_command = (struct load_command *) ((char *) load_command + load_command->cmdsize);
  1054.       }
  1055.     free (hdrbuf);
  1056.     if (!symtab_seen)
  1057.       *syms_offset = *syms_size = *strs_offset = *strs_size = 0;
  1058.     return 1;
  1059.       }
  1060.   }
  1061. #endif
  1062.  
  1063.   return 0;
  1064. }
  1065. #endif /* !atarist */
  1066.  
  1067. /* Output a summary gmon file containing all our accumulated
  1068.    histogram and call-graph data.  */
  1069.  
  1070. void
  1071. write_summary FUN0()
  1072. {
  1073.   struct mesym *p;
  1074.   struct symlist *t;
  1075.   struct gm_call call_tmp;
  1076.   FILE *fp;
  1077.  
  1078. #ifdef atarist
  1079.   fp=ck_fopen (SUM_NAM, "wb");
  1080. #else
  1081.   fp=ck_fopen (SUM_NAM, "w");
  1082. #endif
  1083.   hdr.nbytes=sizeof (struct gm_header)+nhist*sizeof (CHUNK);
  1084.   ck_fwrite ((void *)&hdr, sizeof (hdr), 1, fp);
  1085.   ck_fwrite ((void *)histo, sizeof (unsigned CHUNK), nhist, fp);
  1086.   /* for (p= &syms[nsym]; --p>=&syms[0];) { */
  1087.   if (nsym) {
  1088.     p= &syms[nsym-1];
  1089.     do {
  1090.       for (t=p->calls; t; t=t->next_to) {
  1091.     /* Since we've forgotten exactly where in FROM we
  1092.        were called from, we fake it.  Since this is only
  1093.        gonna be fed back into gprof, it doesn't matter */
  1094. #ifndef atarist
  1095.       call_tmp.from=p->value+FUDGE_FACTOR;
  1096.       call_tmp.to=t->sym_to->value+FUDGE_FACTOR;
  1097. /*    call_tmp.from=(p->value+FUDGE_FACTOR)-hdr.low;
  1098.     call_tmp.to=(t->sym_to->value+FUDGE_FACTOR)-hdr.low; */
  1099. #else
  1100.     call_tmp.from=(p->value+FUDGE_FACTOR)+hdr.low;
  1101.     call_tmp.to=(t->sym_to->value+FUDGE_FACTOR)+hdr.low;
  1102. #endif
  1103.     call_tmp.ncalls=t->ncalls;
  1104.     ck_fwrite ((void *)&call_tmp, sizeof (call_tmp), 1, fp);
  1105.       }
  1106.     } while (p-->&syms[0]);
  1107.   }
  1108.   ck_fclose (fp);
  1109. }
  1110.  
  1111. /* Print the flat profile from the symbol table information.  */
  1112.  
  1113. void
  1114. print_flat_profile FUN0()
  1115. {
  1116.   int n;
  1117.   struct mesym **funp;
  1118.  
  1119.   /* Scan the symbol table and discover how many functions either had time
  1120.      spent in them, or had a non-zero call count */
  1121.   for (n=0; n<nsym; n++) {
  1122.     if (syms[n].histo || syms[n].ncalled || print_zeros)
  1123.       number_of_flat_profile_functions++;
  1124.   }
  1125.   /* Collect all the interesting functions */
  1126.   flat_profile_functions
  1127.     =(struct mesym **)ck_calloc (number_of_flat_profile_functions, sizeof (struct mesym *));
  1128.   for (n=0, funp=flat_profile_functions; n<nsym; n++) {
  1129.     if (syms[n].histo || syms[n].ncalled || print_zeros) {
  1130.       *funp= &syms[n];
  1131.       funp++;
  1132.     }
  1133.   }
  1134.  
  1135.   /* Sort them */
  1136.   qsort (flat_profile_functions, number_of_flat_profile_functions,
  1137.     sizeof (struct mesym *), timecmp);
  1138.  
  1139.   /* And print them out */
  1140.  
  1141.   if (no_blurbs)
  1142.     printf ("\t\tFlat profile\n\n");
  1143.   else
  1144.     printf ("\tFlat profile (explanation follows)\n\n");
  1145.  
  1146.   if (no_blurbs)
  1147.     printf ("\t\tCall graph is on the following page.\n\n");
  1148.   else
  1149.     printf ("\tCall graph is on the following page.\n\n");
  1150.  
  1151.   printf ("Each sample counts as %g seconds.\n\n", 1.0/ticks);
  1152.  
  1153.   puts ("% time  seconds   cumsec   calls  function");
  1154.  
  1155.   for (n=0; n<number_of_flat_profile_functions; n++) {
  1156.     unsigned histo;
  1157.     static cumhist = 0;
  1158.  
  1159.     histo=flat_profile_functions[n]->histo;
  1160.     cumhist+=histo;
  1161.     printf ("%6.2f ", (FLOAT)(100.0*histo)/(FLOAT)tothist);
  1162.     printf ("%8.2f ", ((FLOAT)histo)/(FLOAT)ticks);
  1163.     printf ("%8.2f ", ((FLOAT)cumhist)/(FLOAT)ticks);
  1164.     if (flat_profile_functions[n]->ncalled)
  1165.       printf ("  %5d  ", flat_profile_functions[n]->ncalled);
  1166.     else fputs ("         ", stdout);
  1167.     fprint_name (stdout, flat_profile_functions[n]->name);
  1168.     fputs ("\n", stdout);
  1169.   }
  1170.  
  1171.   /* RMS says we should print the blurb last, which makes sense to me */
  1172.   print_blurb (first_blurb);
  1173. }
  1174.  
  1175. /* Compute the call graph and print it.  */
  1176.  
  1177. void
  1178. print_call_graph FUN0()
  1179. {
  1180.   int n;
  1181.   int index;
  1182.   struct mesym **funp;
  1183.   struct mesym **f;
  1184.  
  1185.   /* Count the functions that appear in the call tree.  */
  1186.   for (n=0; n<nsym; n++) {
  1187.     /* If a function calls something else, or is called by
  1188.        something else, its in the call tree. . . */
  1189.     if (syms[n].ncalls || syms[n].ncalled)
  1190.       number_of_functions_in_call_tree++;
  1191.   }
  1192.  
  1193.   if (number_of_functions_in_call_tree == 0) {
  1194.     printf ("\t\tNo call graph information available.\n");
  1195.     return;
  1196.   }
  1197.  
  1198.   /* Allocate a vector of these functions.  */
  1199.   functions_in_call_tree
  1200.     =(struct mesym **)ck_calloc (number_of_functions_in_call_tree, sizeof (struct mesym *));
  1201.   for (n=0, funp=functions_in_call_tree; n<nsym; n++) {
  1202.     if (syms[n].ncalls || syms[n].ncalled) {
  1203.       *funp= &syms[n];
  1204.       funp++;
  1205.     }
  1206.   }
  1207.  
  1208.   /* Put all the leaf nodes at the end of the call tree */
  1209.   qsort (functions_in_call_tree, number_of_functions_in_call_tree, sizeof (struct mesym *), callcmp);
  1210.  
  1211.   /* Root nodes are now all at the head, and can be easily found
  1212.      'cuz they have call-counts of zero (Never been called, but
  1213.      calls something else; that's spontaneous in my book.) */
  1214.   /* Ordinary nodes are in the middle, (were called, and
  1215.      called others.  Leaf nodes are at the end. */
  1216.  
  1217.   /* Our mission, should we choose to accept it, is to detect
  1218.      circles in the call graph. */
  1219.   /* We do this by keeping a pointer into the call tree called f_end.  This
  1220.      points to the end of the functions that we don't know if they are leaf
  1221.      nodes or not.  When we know something is a leaf node, we move it down
  1222.      past f_end */
  1223.  
  1224.   f= &functions_in_call_tree[number_of_functions_in_call_tree-1];
  1225.   do {
  1226.     if ((*f)->ncalls!=0)
  1227.       break;
  1228.     (*f)->cycnum= -1;
  1229.  
  1230.     /* Note the neeto side effect here!  Is there a better way to do this? */
  1231.   } while (f-- > &functions_in_call_tree[0]);
  1232.  
  1233.   /* Note that functions that only call themselves (and nobody else) don't
  1234.      get marked above.  Doesn't matter; they get marked soon. */
  1235.   if (f== &functions_in_call_tree[0]) {
  1236.     (*f)->cycnum= -1;
  1237.   } else {
  1238.     int found;
  1239.  
  1240.     f_end=f;
  1241.  
  1242.     /* Eliminate recursive calls */
  1243.     do {
  1244.       struct symlist *t, *u;
  1245.  
  1246.       for (t= (*f)->calls; t; t=t->next_to) {
  1247.     if (t->sym_to!= (*f))
  1248.       continue;
  1249.     (*f)->recursive+=t->ncalls;
  1250.  
  1251.     (*f)->ncalls-=t->ncalls;
  1252.     (*f)->ncalled-=t->ncalls;
  1253.  
  1254.     /* Delete from linked list */
  1255.     if (t==(*f)->calls)
  1256.       (*f)->calls=t->next_to;
  1257.     else {
  1258.       for (u=(*f)->calls; u->next_to!=t; u=u->next_to)
  1259.         ;
  1260.       u->next_to=t->next_to;
  1261.     }
  1262.  
  1263.     /* Find and delete from called list too */
  1264.     if (t==(*f)->called)
  1265.       (*f)->called=t->next_from;
  1266.     else {
  1267.       for (u=(*f)->called; u->next_from!=t; u=u->next_from)
  1268.         ;
  1269.       u->next_from=t->next_from;
  1270.     }
  1271.     free (t);
  1272.     break;
  1273.       }
  1274.     } while (f--!=&functions_in_call_tree[0]);
  1275.  
  1276.     number_of_cycles = 0;
  1277.  
  1278.     /* Either there weren't any cycles, or all the cycles live
  1279.        between f_end and functions_in_call_tree */
  1280.  
  1281.     /* Mark all functions that are not in any cycle.  */
  1282.     flushfuns ();
  1283.  
  1284.  
  1285.     /* If we haven't got them all, find the cycle (s).  */
  1286.     if (f_end != &functions_in_call_tree[0])
  1287.       findcycles ();
  1288.   }
  1289.  
  1290.   /* Add entries for the cycles to the vector of all call-graph nodes.  */
  1291.  
  1292.   functions_in_call_tree
  1293.     = (struct mesym **)ck_realloc (functions_in_call_tree,
  1294.         (number_of_cycles+number_of_functions_in_call_tree)*sizeof (struct mesym *));
  1295.   for (n=0; n<number_of_cycles; n++)
  1296.     functions_in_call_tree[number_of_functions_in_call_tree++]= cycles[n];
  1297.  
  1298.   /* Now discard the functions that the filters say should not appear.  */
  1299.  
  1300.   filter_graph ();
  1301.  
  1302.   /* So by now, the only functions left are the ones we want to print */
  1303.   qsort (functions_in_call_tree, number_of_functions_in_call_tree, sizeof (struct mesym *), treetimecmp);
  1304.  
  1305.   /* Assign each function its index number.  */
  1306.   for (n=0; n<number_of_functions_in_call_tree; n++)
  1307.     functions_in_call_tree[n]->numindex=n+1;
  1308.  
  1309.   if (no_blurbs)
  1310.     printf ("\t\t\tCall graph\n\n");
  1311.   else
  1312.     printf ("\t\t     Call graph (explanation follows)\n\n");
  1313.  
  1314.   puts ("index  % time    self  children called     name");
  1315.  
  1316.   /* Loop over entries.  */
  1317.  
  1318.   for (index = 0; index <number_of_functions_in_call_tree; index++) {
  1319.     struct mesym *current = functions_in_call_tree[index];
  1320.     char    tmpstr[40];    /* Can an int have more than 40 digits?  I hope not */
  1321.  
  1322.     /* Print out all the things that called this */
  1323.     if (current->ncalled==0 && current->called==0)
  1324.       printf ("                                             <spontaneous>\n");
  1325.     else {
  1326.       print_sorted_list (current->ncalled, current->cycnum, current->called);
  1327.     }
  1328.         
  1329.         
  1330.     sprintf (tmpstr, "[%d]", current->numindex);
  1331.     printf ("%-6s %6.2f %7.2f  %7.2f ",
  1332.        tmpstr,
  1333.        (FLOAT)(100.0*(current->sub_histo+current->histo))/(FLOAT)tothist,
  1334.        convert_and_round ((FLOAT)current->histo),
  1335.        convert_and_round ((FLOAT)current->sub_histo));
  1336.  
  1337.     if (current->recursive) {
  1338.       printf ("%4d+%-4d ",
  1339.          current->ncalled,
  1340.          current->recursive);
  1341.       fprint_name (stdout, current->name);
  1342.     } else {
  1343.       printf ("%4d      ", current->ncalled);
  1344.       fprint_name (stdout, current->name);
  1345.     }
  1346.  
  1347.     if (current->cycnum>0 && current->name[0]!='<')
  1348.       printf (" <cycle %d>", current->cycnum);
  1349.  
  1350.     printf (" [%d]\n", current->numindex);
  1351.  
  1352.  
  1353.     /* Now print out the children */
  1354.  
  1355.     if (current->name[0]=='<')
  1356.       print_sorted_list (-2, current->cycnum, current->calls);
  1357.     else
  1358.       print_sorted_list (-1, current->cycnum, current->calls);
  1359.  
  1360.  
  1361.     printf ("----------------------------------------\n");
  1362.   }
  1363.  
  1364.   print_blurb (second_blurb);
  1365. }
  1366.  
  1367. /* Scan the call tree for virtual leaf nodes */
  1368. /* If we find any, propagate time into them from their children,
  1369.    mark them as being leaf nodes.  Then repeat the process.  When
  1370.    we drop out of here, either we've flushed the entire tree, or
  1371.    we've found a cycle. */
  1372.  
  1373. void
  1374. flushfuns FUN0()
  1375. {
  1376.   int    found;
  1377.   struct mesym **f;
  1378.  
  1379.   do {
  1380.     found=0;
  1381.     f=f_end;
  1382.     do {
  1383.       struct symlist *t;
  1384.  
  1385.       assert (f>=functions_in_call_tree);
  1386.  
  1387.       for (t=(*f)->calls; t; t=t->next_to)
  1388.     if (t->sym_to->cycnum==0)
  1389.       break;
  1390.  
  1391.       /* We've found an virtual leaf node.  We shold
  1392.      propagate time into the node, and move it
  1393.      past f_end, since we aren't interested in
  1394.      it anymore */
  1395.       if (!t) {
  1396.     found++;
  1397.  
  1398.     /* If its a member of a cycle, cycnum is positive, and
  1399.        time propagation has already been dealt with. */
  1400.     if ((*f)->cycnum==0) {
  1401.       for (t=(*f)->calls; t; t=t->next_to) {
  1402.         struct mesym *symP;
  1403.  
  1404.         if (t->sym_to->name[0]=='<')
  1405.           continue;
  1406.         if (t->sym_to->cycnum==-1)
  1407.           symP= t->sym_to;
  1408.         else 
  1409.           symP= cycles[t->sym_to->cycnum-1];
  1410.  
  1411.         t->prop_time= (FLOAT)t->ncalls*(FLOAT)symP->histo/(FLOAT)symP->ncalled;
  1412.         t->child_time=(FLOAT)t->ncalls* symP->sub_histo  /(FLOAT)symP->ncalled;
  1413.         (*f)->sub_histo+=t->prop_time+t->child_time;
  1414.       }
  1415.       (*f)->cycnum= -1;
  1416.     }
  1417.  
  1418.     /* move this node past f_end, since we don't care
  1419.        about it anymore */
  1420.     if (f_end!=functions_in_call_tree) {
  1421.       /* move this function to the end */
  1422.       if (f!=f_end) {
  1423.         struct mesym *tmp;
  1424.  
  1425.         tmp= *f;
  1426.         *f= *f_end;
  1427.         *f_end=tmp;
  1428.       }
  1429.       --f_end;
  1430.     }
  1431.     assert (f_end>=functions_in_call_tree);
  1432.       }
  1433.     } while (f-->&functions_in_call_tree[0]);
  1434.   } while (found && f_end>&functions_in_call_tree[0]);
  1435. }
  1436.  
  1437. void
  1438. findcycles FUN0()
  1439. {
  1440.   struct mesym **f;
  1441.   struct mesym *ptr;
  1442.   struct symlist *tmp;
  1443.   struct cy {
  1444.     struct mesym *memb1;
  1445.     struct mesym *memb2;
  1446.     struct mesym **others;
  1447.     int width;
  1448.     int deepest;
  1449.     int shallowest;
  1450.   };
  1451.   struct cy *cy;
  1452.   int ncy = 0;
  1453.   int n;
  1454.  
  1455.   int bigdepth = 0;
  1456.   int curdepth;
  1457.   struct mesym *current_cycle_pointer;
  1458.   struct mesym *cursym;
  1459.  
  1460.   int tree_depth;
  1461.  
  1462.   for (f= &functions_in_call_tree[0]; f<=f_end; f++) {
  1463.     if ((*f)->ncalled==0) {
  1464.       push_ring_buffer (ring_buffer, *f);
  1465.       (*f)->flag=0;
  1466.     }
  1467.   }
  1468.   push_ring_buffer (ring_buffer, (void *)0);
  1469.  
  1470.   tree_depth = 1;
  1471.   for (;;) {
  1472.     ptr=pop_ring_buffer (ring_buffer);
  1473.     if (!ptr) {
  1474.       if (ring_buffer_isnt_empty (ring_buffer)) {
  1475.     push_ring_buffer (ring_buffer, (void *)0);
  1476.     tree_depth ++;
  1477.     continue;
  1478.       } else {
  1479.     break;
  1480.       }
  1481.     } else if (ptr->flag==1) {
  1482.       fprintf (stderr, "Ignoring call to spont function ");
  1483.       fprint_name (stderr, ptr->name);
  1484.       fprintf (stderr, "\n");
  1485.     } else if (ptr->flag!=0) {
  1486.       /* Save upward edge */
  1487.       /* printf ("Upward edge detected in function %s\n", ptr->name); */
  1488.       if (!ncy) {
  1489.     cy=ck_malloc (sizeof (struct cy));
  1490.     ncy=1;
  1491.       } else {
  1492.     ncy++;
  1493.     cy=ck_realloc (cy, ncy*sizeof (struct cy));
  1494.       }
  1495.       cy[ncy-1].memb1=ptr;
  1496.       cy[ncy-1].memb2=0;
  1497.     } else {
  1498.       ptr->flag=tree_depth;
  1499.       for (tmp=ptr->calls; tmp; tmp=tmp->next_to)
  1500.     if (tmp->sym_to->cycnum!=-1)
  1501.       push_ring_buffer (ring_buffer, tmp->sym_to);
  1502.     }
  1503.   }
  1504.   if (!ncy)
  1505.     return;
  1506.  
  1507.   for (n=0; n<ncy; n++) {
  1508.     struct symlist *s;
  1509.     struct symlist *u;
  1510.  
  1511.  
  1512.     cursym=cy[n].memb1;
  1513.     if (cursym->cycnum)
  1514.       continue;
  1515.  
  1516.     number_of_cycles++;
  1517.     /* for (s=cursym->called; s; s=s->next_from) {
  1518.        if (s->sym_from->flag>=cursym->flag)
  1519.        break;
  1520.        }
  1521.        if (!s)
  1522.        exit (92); */
  1523.  
  1524.     if (!cycles)
  1525.       cycles=(struct mesym **)ck_malloc (sizeof (struct mesym *));
  1526.     else
  1527.       cycles=(struct mesym **)ck_realloc ((void *)cycles,
  1528.                       number_of_cycles*sizeof (struct mesym *));
  1529.  
  1530.     current_cycle_pointer = (struct mesym *)ck_malloc (sizeof (struct mesym));
  1531.     cycles[number_of_cycles-1] = current_cycle_pointer;
  1532.     bzero (current_cycle_pointer, sizeof *current_cycle_pointer);
  1533.     current_cycle_pointer->name=mk_sprintf ("<cycle %d as a whole>", number_of_cycles);
  1534.     current_cycle_pointer->value= (unsigned long)-1;
  1535.     current_cycle_pointer->cycnum=number_of_cycles;
  1536.  
  1537.     cursym=cy[n].memb1;
  1538.  
  1539.     push_ring_buffer (ring_buffer, cursym);
  1540.  
  1541.     while (ring_buffer_isnt_empty (ring_buffer)) {
  1542.       cursym=pop_ring_buffer (ring_buffer);
  1543.       if (cursym->cycnum==number_of_cycles)
  1544.     continue;
  1545.       if (cursym->cycnum!=0)
  1546.     exit (93);
  1547.       PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_CYC, ("adding %s (%d) to cycle", cursym->name, cursym->histo));
  1548.       current_cycle_pointer->histo+=cursym->histo;
  1549.       cursym->cycnum=number_of_cycles;
  1550.       add_to_lists (current_cycle_pointer, cursym, 0);
  1551.  
  1552.       /* Now queue the subroutines of this function to be scanned
  1553.      eventually.  */
  1554.  
  1555.       for (u=cursym->calls; u; u=u->next_to)
  1556.     if (u->sym_to->cycnum==0) {
  1557.       push_ring_buffer (ring_buffer, (void *)(u->sym_to));
  1558.     } else if (u->sym_to->name[0] == '<') {
  1559.       PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_CYC, ("Cycle calls %s (%d)", u->sym_to->name, u->ncalls));
  1560.       add_to_lists (current_cycle_pointer, u->sym_to, u->ncalls);
  1561.     }
  1562.     }
  1563.  
  1564.     /* The cycle's list of children now contains all the members of the cycle.
  1565.        Occasionally a function creeps in that doesn't belong in the cycle.
  1566.        Find and remove them. */
  1567.  
  1568.     {
  1569.       struct symlist *v;
  1570.       int found;
  1571.  
  1572.       do {
  1573.     found = 0;
  1574.     for (u=current_cycle_pointer->calls; u; u=u->next_to) {
  1575.       for (v=u->sym_to->called; v; v=v->next_from)
  1576.         if (v->sym_from->name[0]!='<' && v->sym_from->cycnum==number_of_cycles)
  1577.           break;
  1578.       if (!v) {
  1579.         PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_CYC, ("%s not really in cycle", u->sym_to->name));
  1580.         /* This 'cycle member' wasn't called by any member of the cycle.
  1581.            thus, it isn't a cycle member. */
  1582.         u->sym_to->cycnum=0;
  1583.         delete_from_lists (current_cycle_pointer, u->sym_to);
  1584.         found++;
  1585.       }
  1586.     }
  1587.       } while (found);
  1588.     }
  1589.     /* The cycle's lists of callers and children are now correct.
  1590.        Propagate the time through the cycle.  */
  1591.  
  1592.  
  1593.     /* Now we propagate time INTO the
  1594.        cycle from its children.  We could do this in flushfuns,
  1595.        but its easier to do here, since doing it here guarentees
  1596.        it only happens once per cycle */
  1597.  
  1598.     for (u=current_cycle_pointer->calls; u; u=u->next_to) {
  1599.       struct mesym *symP;
  1600.       struct symlist *v;
  1601.  
  1602.       /* We have to remember to not propagate time that's already here. . . */
  1603.       if (u->sym_to->cycnum!=number_of_cycles) {
  1604.     current_cycle_pointer->ncalls+=u->ncalls;
  1605.  
  1606.     if (u->sym_to->cycnum==-1)
  1607.       symP= u->sym_to;
  1608.     else            /* Propagate time FROM another cycle here */
  1609.       symP= cycles[u->sym_to->cycnum-1];
  1610.     u->prop_time= (FLOAT)u->ncalls*(FLOAT)symP->histo/(FLOAT)symP->ncalled;
  1611.     u->child_time=(FLOAT)u->ncalls* symP->sub_histo  /(FLOAT)symP->ncalled;
  1612.     current_cycle_pointer->sub_histo+=u->prop_time+u->child_time;
  1613.     for (v=u->sym_to->called; v; v=v->next_from) {
  1614.       if (v->sym_from->cycnum==number_of_cycles) {
  1615.         v->prop_time= (FLOAT)v->ncalls*(FLOAT)symP->histo/(FLOAT)symP->ncalled;
  1616.         v->child_time=(FLOAT)v->ncalls*  symP->sub_histo /(FLOAT)symP->ncalled;
  1617.       }
  1618.     }
  1619.       } else {
  1620.     u->prop_time= u->sym_to->histo;
  1621.     u->child_time=u->sym_to->sub_histo;
  1622.  
  1623.     for (v=u->sym_to->calls; v; v=v->next_to) {
  1624.       if (v->sym_to->cycnum==number_of_cycles) {
  1625.         add_to_lists (current_cycle_pointer, v->sym_to, v->ncalls);
  1626.         current_cycle_pointer->recursive+=v->ncalls;
  1627.       } else {
  1628.         symP = v->sym_to;
  1629.         v->prop_time= (FLOAT)v->ncalls*(FLOAT)symP->histo/(FLOAT)symP->ncalled;
  1630.         v->child_time=(FLOAT)v->ncalls*  symP->sub_histo /(FLOAT)symP->ncalled;
  1631.       }
  1632.     }
  1633.  
  1634.     for (v=u->sym_to->called; v; v=v->next_from) {
  1635.       assert (v->sym_from->cycnum==0 || v->sym_from->cycnum==number_of_cycles);
  1636.       if (v->sym_from->cycnum==0) {
  1637.         PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_CYC, ("Cycle called by %s (%d)", v->sym_from->name, v->ncalls));
  1638.         add_to_lists (v->sym_from, current_cycle_pointer, v->ncalls);
  1639.       }
  1640.     }
  1641.       }
  1642.     }
  1643.  
  1644.  
  1645.     /* Sum all calls into the cycle, from each caller not in the cycle,
  1646.        to get the number of calls into the cycle.  */
  1647.     for (u=current_cycle_pointer->called; u; u=u->next_from) {
  1648.       if (u->sym_from->cycnum!=number_of_cycles)
  1649.     current_cycle_pointer->ncalled+=u->ncalls;
  1650.     }
  1651.  
  1652.     /* Loop over functions in this cycle.  */
  1653.     for (u=current_cycle_pointer->calls; u; u=u->next_to) {
  1654.       struct symlist *v;
  1655.  
  1656.       if (u->sym_to->cycnum!=number_of_cycles)
  1657.     continue;
  1658.  
  1659.       /* U->sym_to is a function in this cycle.  */
  1660.  
  1661.       /* Find all calls to this function from functions outside the cycle
  1662.      and add remove them from the count of calls "from the cycle" to this function.  */
  1663.  
  1664.       for (v=u->sym_to->called; v; v=v->next_from) {
  1665.     if (v->sym_from != current_cycle_pointer
  1666.         && v->sym_from->cycnum==number_of_cycles)
  1667.       u->sym_to->ncalled-=v->ncalls;
  1668.       }
  1669.     }
  1670.  
  1671.     /* Compute amounts of time to propagate out of the cycle
  1672.        to the callers-in.  */
  1673.  
  1674.     for (u=current_cycle_pointer->called; u; u=u->next_from)
  1675.       if (u->sym_from->cycnum != number_of_cycles) {
  1676.     struct symlist *v;
  1677.  
  1678.     u->prop_time= ((FLOAT)u->ncalls*(FLOAT)current_cycle_pointer->histo     /(FLOAT)current_cycle_pointer->ncalled);
  1679.     u->child_time=((FLOAT)u->ncalls*       current_cycle_pointer->sub_histo /(FLOAT)current_cycle_pointer->ncalled);
  1680.     for (v=u->sym_from->calls; v; v=v->next_to) {
  1681.       if (v->sym_to->cycnum==number_of_cycles) {
  1682.         v->prop_time= ((FLOAT)v->ncalls*(FLOAT)current_cycle_pointer->histo     /(FLOAT)current_cycle_pointer->ncalled);
  1683.         v->child_time=((FLOAT)v->ncalls*       current_cycle_pointer->sub_histo /(FLOAT)current_cycle_pointer->ncalled);
  1684.       }
  1685.     }
  1686.       }
  1687.  
  1688.     flushfuns ();
  1689.   }
  1690. }
  1691.  
  1692.  
  1693. /* Remove from the call tree all nodes that are rejected by
  1694.    the -e, -E, -f and -F filters that were specified.
  1695.    Their nodes are removed from functions_in_call_tree
  1696.    and their edges are deleted from the lists they are in.  */
  1697.  
  1698. void
  1699. filter_graph FUN0()
  1700. {
  1701.   int n;
  1702.   int the_bomb_is_falling = 0;
  1703.  
  1704.   for (n=0; n<nfilters; n++) {
  1705.     struct mesym **call_tree_pointer;
  1706.  
  1707.     call_tree_pointer=find_funp_from_name (filters[n].name);
  1708.     /* Couldn't find it?  Skip it! */
  1709.     if (!call_tree_pointer) {
  1710.       /* It may have taken time, although it
  1711.      isn't in the call tree.  Seek and
  1712.      destroy! */
  1713.       if (filters[n].type==BIG_E
  1714.       || filters[n].type == REMOVE_TIME_IF_THERE) {
  1715.     struct mesym *p;
  1716.  
  1717.     for (p=syms; p< &syms[nsym]; p++) {
  1718. #ifndef atarist
  1719.       if (!strcmp (p->name, filters[n].name)) {
  1720. #else
  1721.       if (!strncmp (p->name, filters[n].name, max_atari_sym_length)) {
  1722. #endif
  1723.         tothist-=p->histo;
  1724.         break;
  1725.       }
  1726.     }
  1727.     if (p==&syms[nsym] && filters[n].type != REMOVE_TIME_IF_THERE) {
  1728.       fprintf (stderr, "Warning: couldn't find function ");
  1729.       fprint_name (stderr, filters[n].name);
  1730.       fprintf (stderr, "\n");
  1731.     }
  1732.       } else {
  1733.     fprintf (stderr, "Warning:  ");
  1734.     fprint_name (stderr, filters[n].name);
  1735.     fprintf (stderr, " is not in the call tree.\n");
  1736.       }
  1737.       continue;
  1738.     }
  1739.     PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_OPT, ("Option %d on %s", filters[n].type, (*call_tree_pointer)->name));
  1740.  
  1741.     switch (filters[n].type) {
  1742.     case SMALL_E:
  1743.       kill_children (*call_tree_pointer, FALSE);
  1744.       break;
  1745.     case BIG_E:
  1746.     case REMOVE_TIME_IF_THERE:
  1747.       kill_children (*call_tree_pointer, TRUE);
  1748.       break;
  1749.     case SMALL_F:
  1750.       if (!the_bomb_is_falling)
  1751.     the_bomb_is_falling = 1;
  1752.       save_the_children (*call_tree_pointer, FALSE);
  1753.       break;
  1754.     case BIG_F:
  1755.       if (!the_bomb_is_falling) {
  1756.     the_bomb_is_falling = 1;
  1757.     tothist=0;
  1758.       }
  1759.       save_the_children (*call_tree_pointer, TRUE);
  1760.       break;
  1761.     default:
  1762.       exit (94);
  1763.     }
  1764.   }
  1765.  
  1766.   /* If we had -e or -E filters, now delete everything that
  1767.      was not marked to be saved.  */
  1768.  
  1769.   if (the_bomb_is_falling) {
  1770.     struct mesym **call_tree_pointer;
  1771.  
  1772.     PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_OPT, ("And now we drop the bomb."));
  1773.     call_tree_pointer =
  1774.     &functions_in_call_tree[number_of_functions_in_call_tree-1];
  1775.     do {
  1776.       if (((*call_tree_pointer)->flag&SAVE_ME)==0)
  1777.     remove_from_call_tree (call_tree_pointer);
  1778.     } while (call_tree_pointer-->&functions_in_call_tree[0]);
  1779.   }
  1780. }
  1781.  
  1782. /* Collect a list of parents/children, sort them, and print them */
  1783. /* If CALLED > 0, we print parents,
  1784.       and the value of CALLED is the total number of times this fn was called.
  1785.    If CALLED == -2 we print children.
  1786.       This is used for printing the children of an entire cycle.
  1787.    If CALLED == -1, we print children and if their cycle number is the
  1788.       same as CYCLE, we print abbreviated information.
  1789.       This is used for printing the children of an ordinary function.  */
  1790. /* LIST is the list of parents/children we want to print */
  1791.  
  1792. void
  1793. print_sorted_list FUN3(int, called, int, cycle, struct symlist *, list)
  1794. {
  1795.   static struct symlist **sbuf;
  1796.   static nsbuf, sizsbuf;
  1797.   struct symlist **sbp, *t;
  1798.   struct mesym *symP;
  1799.  
  1800.   if (!sizsbuf) {
  1801.     sbuf=(struct symlist **)ck_calloc (30, sizeof (struct symlist *));
  1802.     nsbuf=0;
  1803.     sizsbuf=30;
  1804.   }
  1805.  
  1806.   /* Extract all the symbols in LIST as a vector,
  1807.      but ignore any which represent entire cycles.  */
  1808.  
  1809.   t=list;
  1810.   for (sbp= &sbuf[0]; t;) {
  1811.     symP = (called>=0) ? t->sym_from : t->sym_to;
  1812.     if (symP && symP->name[0] != '<') {
  1813.       *sbp=t;
  1814.       sbp++;
  1815.       nsbuf++;
  1816.       if (nsbuf==sizsbuf) {
  1817.     sizsbuf*=2;
  1818.     sbuf=(struct symlist **)ck_realloc ((void *)sbuf, sizsbuf*sizeof (struct symlist *));
  1819.     sbp= &sbuf[nsbuf];
  1820.       }
  1821.     } else {
  1822.       symP++;
  1823.             
  1824.     }
  1825.  
  1826.     if (called>=0) t=t->next_from;
  1827.     else t=t->next_to;
  1828.   }
  1829.  
  1830.   /* Sort the vector.  */
  1831.   qsort (sbuf, nsbuf, sizeof (struct symlist *), listcmp);
  1832.  
  1833.   /* Print the elements of the vector.  */
  1834.   for (sbp= &sbuf[0]; nsbuf>0; nsbuf--, sbp++) {
  1835.     t= *sbp;
  1836.     if (called>=0) symP=t->sym_from;
  1837.     else symP=t->sym_to;
  1838.  
  1839.     if (cycle>0 && cycle==symP->cycnum) {
  1840.       if (called==-2) {
  1841.     printf ("              %7.2f  %7.2f %4d          ",
  1842.            convert_and_round ((FLOAT)(symP->histo)),
  1843.            convert_and_round ((FLOAT)(symP->sub_histo)),
  1844.            t->ncalls);
  1845.     fprint_name (stdout, symP->name);
  1846.       } else {
  1847.     /* For things in the same cycle, we only want to print
  1848.        the number of calls */
  1849.     printf ("%30s %4d          ", "", t->ncalls);
  1850.         fprint_name (stdout, symP->name);
  1851.       }
  1852.     } else {
  1853.       printf ("              %7.2f  %7.2f %4d/%-4d     ",
  1854.          convert_and_round (t->prop_time),
  1855.          convert_and_round (t->child_time),
  1856.          t->ncalls,
  1857.          (called>=0) ? called : symP->ncalled);
  1858.       fprint_name (stdout, symP->name);
  1859.     }
  1860.  
  1861.     if (symP->cycnum>0 && symP->name[0]!='<')
  1862.       printf (" <cycle %d>", symP->cycnum);
  1863.  
  1864.     if (symP->numindex)
  1865.       printf (" [%d]\n", symP->numindex);
  1866.     else
  1867.       printf (" [not printed]\n");
  1868.   }
  1869. }
  1870.  
  1871. /* Compare two symbols for which should come first among
  1872.    the callers or subroutines in a single call-graph entry.  */
  1873.  
  1874. int
  1875. listcmp FUN2(const void *, a, const void *, b)
  1876. {
  1877.   struct symlist *aa, *bb;
  1878.   FLOAT    n;
  1879.  
  1880.   aa= *(struct symlist **)a;
  1881.   bb= *(struct symlist **)b;
  1882.  
  1883.   n=(bb->prop_time + bb->child_time - aa->prop_time - aa->child_time);
  1884.  
  1885.   if (n<0) return -1;
  1886.   if (n>0) return 1;
  1887.   return 0;
  1888. }
  1889.  
  1890. /* Convert a number (IN) from the histogram into seconds, rounding to the
  1891.    nearest 100th of a second. */
  1892. FLOAT
  1893. convert_and_round FUN1(FLOAT, in)
  1894. {
  1895.   long int inter;
  1896.  
  1897.   inter=((in/(FLOAT)(ticks))+.005)*100.0;
  1898.   return ((FLOAT)(inter)/100.0);
  1899. }
  1900.  
  1901. /* Given ncalls from fromP to toP, add a symlist element telling about it */
  1902. void
  1903. add_to_lists FUN3(struct mesym *, fromP, struct mesym *, toP, unsigned, ncalls)
  1904. {
  1905.   struct symlist *tmp;
  1906.  
  1907.   for (tmp=fromP->calls; tmp; tmp=tmp->next_to)
  1908.     if (tmp->sym_to==toP) {
  1909.       tmp->ncalls+=ncalls;
  1910.       break;
  1911.     }
  1912.   if (!tmp) {
  1913.     tmp=(struct symlist *)ck_malloc (sizeof (struct symlist));
  1914.     tmp->sym_from=fromP;
  1915.     tmp->next_from=toP->called;
  1916.     tmp->sym_to=toP;
  1917.     tmp->next_to=fromP->calls;
  1918.     tmp->ncalls=ncalls;
  1919.     tmp->prop_time = -1;
  1920.     tmp->child_time = -1;
  1921.  
  1922.     fromP->calls=tmp;
  1923.     toP->called=tmp;
  1924.   }
  1925. }
  1926.  
  1927.  
  1928. /* The reverse of add_to_lists.  Forget that fromP ever called toP */
  1929. void
  1930. delete_from_lists FUN2(struct mesym *, fromP, struct mesym *, toP)
  1931. {
  1932.   struct symlist *die, *tmp, *old;
  1933.  
  1934.   if (fromP->calls->sym_to==toP) {
  1935.     die=fromP->calls;
  1936.     fromP->calls=fromP->calls->next_to;
  1937.   } else {
  1938.     old=0;
  1939.     for (tmp=fromP->calls; tmp; tmp=tmp->next_to) {
  1940.       if (tmp->sym_to==toP) {
  1941.         die=tmp;
  1942.     old->next_to=tmp->next_to;
  1943.       }
  1944.       old=tmp;
  1945.     }
  1946.   }
  1947.  
  1948.   if (toP->called->sym_from==fromP) {
  1949.     die=toP->called;
  1950.     toP->called=toP->called->next_from;
  1951.   } else {
  1952.     for (tmp=toP->called; tmp; tmp=tmp->next_from) {
  1953.       old=0;
  1954.       if (tmp->sym_from==fromP) {
  1955.         die=tmp;
  1956.     old->next_from=tmp->next_from;
  1957.       }
  1958.       old=tmp;
  1959.     }
  1960.   }
  1961.   free (die);
  1962. }
  1963.  
  1964.  
  1965. /* Implement the -e or -E option by deleting a certain function
  1966.    and all its descendents from the call graph.
  1967.    If FLUSHFLAG is set, remove its histogram time from the total, too. */
  1968.  
  1969. void
  1970. kill_children FUN2(struct mesym *, p, int, flushflag)
  1971. {
  1972.   struct symlist *t;
  1973.  
  1974.   push_ring_buffer (ring_buffer, p);
  1975.   while (ring_buffer_isnt_empty (ring_buffer)) {
  1976.     p=pop_ring_buffer (ring_buffer);
  1977.     if (flushflag)
  1978.       tothist-=p->histo;
  1979.     PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_OPT, ("Flushing %s (%d)", p->name, flushflag ? p->histo : 0));
  1980.     p->flag|=KILL_ME;
  1981.     remove_from_call_tree (find_funp_from_pointer (p));
  1982.     for (t=p->calls; t; t=t->next_to) {
  1983.       if (t->sym_to->ncalled==t->ncalls
  1984.       || (p->cycnum>0 && t->sym_to->cycnum==p->cycnum)) {
  1985.     push_ring_buffer (ring_buffer, t->sym_to);
  1986.       } else if (t->sym_to->cycnum>0 && t->sym_to->cycnum!=p->cycnum) {
  1987.     if (cycles[t->sym_to->cycnum-1]->ncalled==t->ncalls) {
  1988.       push_ring_buffer (ring_buffer, cycles[t->sym_to->cycnum-1]);
  1989.     }
  1990.       } else {
  1991.     struct symlist *ztmp;
  1992.  
  1993.     for (ztmp=t->sym_to->called; ztmp; ztmp=ztmp->next_from) {
  1994.       if ((ztmp->sym_from->flag&KILL_ME)==0)
  1995.         break;
  1996.     }
  1997.     if (!ztmp)
  1998.       push_ring_buffer (ring_buffer, t->sym_to);
  1999.     else {
  2000.       PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_OPT, ("Not flushing %s. . .  More parents", t->sym_to->name));
  2001.       /* Do we want to deal with removing FRACTIONS of time from
  2002.          functions who were called from different places?  If so,
  2003.          code goes here.  (F'rinstance, if half of the calls to
  2004.          function X were made from here, cut its stored time in half */
  2005.     }
  2006.       }
  2007.     }
  2008.   }
  2009. }
  2010.  
  2011. /* This is the opposite of kill_children ().
  2012.    -f or -F has been specified, so all call-graph nodes EXCEPT
  2013.    the descendents of specified functions will be killed.
  2014.    Find all these descendents and mark them to be saved
  2015.    by setting the `flag' fields nonzero.
  2016.  
  2017.    If TIMEFLAG is set, the histogram-total has
  2018.    already been nuked, and we should add our histogram time to
  2019.    it in an attempt at reconstruction. . .  */
  2020.  
  2021. void
  2022. save_the_children FUN2(struct mesym *, p, int,  timeflag)
  2023. {
  2024.   struct symlist *t;
  2025.  
  2026.   push_ring_buffer (ring_buffer, p);
  2027.   while (ring_buffer_isnt_empty (ring_buffer)) {
  2028.     p=pop_ring_buffer (ring_buffer);
  2029.     PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_OPT, ("Saving %s (%d)", p->name, p->histo));
  2030.     p->flag|=SAVE_ME;
  2031.     if (p->cycnum>0)
  2032.       cycles[p->cycnum-1]->flag|=SAVE_ME;
  2033.     if (timeflag)
  2034.       tothist+=p->histo;
  2035.     for (t=p->calls; t; t=t->next_to) {
  2036.       if ((t->sym_to->flag&SAVE_ME)==0)
  2037.     push_ring_buffer (ring_buffer, t->sym_to);
  2038.     }
  2039.   }
  2040. }
  2041.  
  2042. /* The bomb is falling, and PT has just been hit by severe doses of
  2043.    radiation.  Remove it from the call-tree vector */
  2044. void
  2045. remove_from_call_tree FUN1(struct mesym **, pt)
  2046. {
  2047.   if (!pt) {
  2048.     /* Just quietly returning allows us to remove cycles from
  2049.        the call tree easily. */
  2050.     /* panic ("Internal Error:  trying to remove null from call tree"); */
  2051.     return;
  2052.   }
  2053.   PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_OPT, ("Removing %ss from the tree", (*pt)->name));
  2054.   *pt=functions_in_call_tree[number_of_functions_in_call_tree-1];
  2055.   number_of_functions_in_call_tree--;
  2056. }
  2057.  
  2058. /* We want to play with a member of the call tree, but we
  2059.    only know its name.  Try to find the funp.  This is slow,
  2060.    natch, since it uses linear search, but it doesn't get called much */
  2061.  
  2062. /* Should be some way to scan rest of symbols so we could delete
  2063.    mcount/mcleanup histogram ticks.  Should be way to disable warning? */
  2064. struct mesym **
  2065. find_funp_from_name FUN1(char *, name)
  2066. {
  2067.   struct mesym **ret;
  2068.  
  2069.   ret= &functions_in_call_tree[number_of_functions_in_call_tree-1];
  2070.   do {
  2071.     if (strcmp ((*ret)->name, name)==0)
  2072.       return ret;
  2073.   } while (ret-->&functions_in_call_tree[0]);
  2074.   return 0;
  2075. }
  2076.  
  2077. /* We have the mesym, but we need to know where it lives in the call-tree
  2078.    This is almost as slow as find_funp_from_name (). */
  2079. struct mesym **
  2080. find_funp_from_pointer FUN1(struct mesym *, p)
  2081. {
  2082.   struct mesym **ret;
  2083.  
  2084.   ret= &functions_in_call_tree[number_of_functions_in_call_tree-1];
  2085.   do {
  2086.     if (*ret==p)
  2087.       return ret;
  2088.   } while (ret-->&functions_in_call_tree[0]);
  2089.   /* fprintf (stderr, "Warning:  Can't find %s in the call tree.\n", p->name); */
  2090.   return 0;
  2091. }
  2092.  
  2093. #ifndef atarist /* atarist specific version at end */
  2094.  
  2095. /* Read symbols from a.out file open on FP.  There are N of them.  Allocate a
  2096.    vector to store the interesting ones in, and sort it numerically.  */
  2097.  
  2098. void
  2099. read_syms FUN2(FILE *, fp, int, n)
  2100. {
  2101.   struct nlist *tmpsyms;
  2102.   struct nlist *sym;
  2103.   int i;
  2104. #ifdef __STDC__
  2105.   char buf[n];
  2106. #else
  2107.   char *buf;
  2108.   char *alloca ();
  2109.  
  2110.   buf=alloca (n);
  2111. #endif
  2112.   /* Read the entire symbol table.  */
  2113.   tmpsyms=(struct nlist *)ck_malloc (n*sizeof (struct nlist));
  2114.   ck_fread (tmpsyms, sizeof (struct nlist), n, fp);
  2115.  
  2116.   bzero (buf, n);
  2117.  
  2118.   /* Count the useful symbols.
  2119.      Also relocate their name-fields to be C string pointers.  */
  2120.   for (sym= &tmpsyms[0], i=0; sym<&tmpsyms[n]; sym++) {
  2121.     sym->n_un.n_name= strs+sym->n_un.n_strx;
  2122.     if (!badsym (sym))
  2123.       buf[sym-tmpsyms] = 1, i++;
  2124.   }
  2125.  
  2126.   /* Allocate permanent space and copy useful symbols into it.  */
  2127.   nsym=i;
  2128.   syms=(struct mesym *)ck_calloc (nsym, sizeof (struct mesym));
  2129.  
  2130.   for (sym= &tmpsyms[0], i=0; sym<&tmpsyms[n]; sym++) {
  2131.     if (!badsym (sym)) {
  2132.       syms[i].name=sym->n_un.n_name;
  2133.       syms[i].value=sym->n_value;
  2134.       i++;
  2135.     }
  2136.     else if (buf[sym-tmpsyms])
  2137.       exit (95);
  2138.   }
  2139.  
  2140.   if (i != nsym)
  2141.     exit (96);
  2142.  
  2143.   /* Put symbols in numeric order.  */
  2144.   qsort (syms, nsym, sizeof (struct mesym), symcmp);
  2145.   free ((void *)tmpsyms);
  2146. }
  2147. #endif /* atarist */
  2148.  
  2149. /* Return the symbol which has the largest value less than or equal to VAL.
  2150.    Since the symbol vector is sorted by value, this is done
  2151.    with a binary search.  */
  2152.  
  2153. struct mesym *
  2154. val_to_sym FUN1(unsigned long, val)
  2155. {
  2156.   struct mesym *m;
  2157.   int gap=nsym/4;
  2158.  
  2159.   m= &syms[nsym/2];
  2160.   for (;;) {
  2161.     if (m->value>val) {
  2162.       m-=gap;
  2163.       gap/=2;
  2164.     } else if ((m + 1)->value == val)
  2165.     {
  2166.       return(m+1);
  2167.     } else if ((m+1)->value<val) {
  2168.       m+=gap;
  2169.       gap/=2;
  2170.     } else
  2171.       break;
  2172.     if (m<&syms[0] || m>=&syms[nsym])
  2173.       exit (97);
  2174.     if (gap<1)
  2175.       gap=1;
  2176.   }
  2177.   return m;
  2178. }
  2179.  
  2180. #ifndef atarist
  2181. /* Return TRUE if the nlist-entry SYM describes a symbol
  2182.    that gprof should NOT pay attention to.  */
  2183.  
  2184. int
  2185. badsym FUN1(struct nlist *, sym)
  2186. {
  2187.   int local;
  2188. #ifndef N_SECT
  2189.   if ((sym->n_type & ~N_EXT) != N_TEXT)
  2190.     return TRUE;
  2191. #else
  2192.   if ((sym->n_type & ~N_EXT) != N_TEXT && (sym->n_type & ~N_EXT) != N_SECT)
  2193.     return TRUE;
  2194. #endif
  2195.   local = !(sym->n_type&N_EXT);
  2196.   if (no_locals && !local)
  2197.     return TRUE;
  2198.   /* Filenames or pascal labels should be ignored */
  2199. if (local
  2200.     && (index (sym->n_un.n_name, '.')
  2201.       || index (sym->n_un.n_name, '$')
  2202. #ifndef nounderscore
  2203.       || sym->n_un.n_name[0] != '_'
  2204. #endif
  2205.       ))
  2206.     return TRUE;
  2207.   return FALSE;
  2208. }
  2209. #endif
  2210.  
  2211. /* This is used to qsort () the symbol table into numerical order. */
  2212.  
  2213. int
  2214. symcmp FUN2(const void *, a, const void *, b)
  2215. {
  2216.   struct mesym *aa, *bb;
  2217.  
  2218.   aa=(struct mesym *)a;
  2219.   bb=(struct mesym *)b;
  2220.   return aa->value - bb->value;
  2221. }
  2222.  
  2223. /* This is used to sort the vector for the flat profile.
  2224.    if they used different amounts of time, the one with
  2225.    the most time goes first.  If they used the same amount,
  2226.    the one that was called most goes first.  If they were
  2227.    called the same #, they are sorted alphabetically */
  2228.  
  2229. int
  2230. timecmp FUN2(const void *, a, const void *, b)
  2231. {
  2232.   struct mesym *aa, *bb;
  2233.   int    n;
  2234.  
  2235.   aa= *(struct mesym **)a;
  2236.   bb= *(struct mesym **)b;
  2237.   n = bb->histo - aa->histo;
  2238.   if (n==0) n=bb->ncalled - aa->ncalled;
  2239.   if (n==0) n=strcmp (aa->name, bb->name);
  2240.   return n;
  2241. }
  2242.  
  2243. /* This is used to sort the functions_in_call_tree[] vector
  2244.    so the leaf nodes are at the end, and the root nodes
  2245.    are at the beginning.  This bit about useless nodes could
  2246.    probably be taken out now, since they shouldn't make it
  2247.    into the vector in the first place */
  2248.  
  2249. int
  2250. callcmp FUN2(const void *, a, const void *, b)
  2251. {
  2252.   struct mesym *aa, *bb;
  2253.  
  2254.   aa= *(struct mesym **)a;
  2255.   bb= *(struct mesym **)b;
  2256.  
  2257.   /* Send useless symbols to the end */
  2258.   if (aa->ncalls==0 && aa->ncalled==0) {
  2259.     if (bb->ncalls==0 && bb->ncalled==0)
  2260.       return EQ;
  2261.     return GT;
  2262.   }
  2263.   if (bb->ncalled==0 && bb->ncalls==0)
  2264.     return LT;
  2265.  
  2266.   /*     send root nodes to the front; */
  2267.   /* Functions that were called 0 times
  2268.      are spontaneous */
  2269.  
  2270.   if (aa->ncalled==0) {
  2271.     if (bb->ncalled==0)
  2272.       return EQ;
  2273.     return LT;
  2274.   }
  2275.   if (bb->ncalled==0)
  2276.     return GT;
  2277.  
  2278.   /* Send leaf nodes to the end */
  2279.   if (aa->ncalls==0) {
  2280.     if (bb->ncalls==0)
  2281.       return EQ;
  2282.     return GT;
  2283.   }
  2284.   if (bb->ncalls==0)
  2285.     return LT;
  2286.  
  2287.   /* And keep the rest the same */
  2288.   return EQ;
  2289. }
  2290.  
  2291. /* This is used to sort functions_in_call_tree[] before printing.
  2292.    To print, we want
  2293.    A:    the ones with the most time first
  2294.        a:    (If one was never called, put it first, 'cuz it
  2295.         was invoked by GOD, else the one called the most
  2296.          # of times first)
  2297.         1:  the one with the lower name (strcmp ()) first
  2298.  */
  2299.  
  2300. int
  2301. treetimecmp FUN2(const void *, a, const void *, b)
  2302. {
  2303.   struct mesym *aa, *bb;
  2304.   int    n;
  2305.  
  2306.   aa= *(struct mesym **)a;
  2307.   bb= *(struct mesym **)b;
  2308.   n = (bb->histo+bb->sub_histo) - (aa->histo+aa->sub_histo);
  2309.   if (n==0) {
  2310.  
  2311.     /* Just to be bizarre:  If one was never called,
  2312.        put it first, else put the one who was called
  2313.        the most first.  Confused yet? */
  2314.     if (aa->ncalled==0)
  2315.       return LT;
  2316.     if (bb->ncalled==0)
  2317.       return GT;
  2318.     n=bb->ncalled - aa->ncalled;
  2319.     if (n==0)
  2320.       n=strcmp (aa->name, bb->name);
  2321.   }
  2322.   return n;
  2323. }
  2324.  
  2325. /* Read in a gmon.out file named NAME and deal with the stuff inside it.  */
  2326.  
  2327. void
  2328. readgm FUN1(char *, name)
  2329. {
  2330.   FILE *fp;
  2331.   struct gm_header tmp;
  2332.   unsigned CHUNK *p;
  2333.   int    n;
  2334.   struct gm_call calltmp;
  2335.  
  2336.   PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_GFILE, ("gmon from `%s'", name));
  2337.  
  2338. #ifdef atarist
  2339.   fp=ck_fopen (name, "rb");
  2340. #else
  2341.   fp=ck_fopen (name, "r");
  2342. #endif
  2343.  
  2344.   /* Read in the gmon file header and check that histogram is
  2345.      compatible with the other gmon files already read.  */
  2346.  
  2347.   ck_fread ((void *)&tmp, sizeof (tmp), 1, fp);
  2348.   if (hdr.low) {
  2349.     if (hdr.low!=tmp.low || hdr.high!=tmp.high)
  2350.       fatal ("file `%s' is incompatable with previous gmon.out file", name);
  2351.   } else
  2352.     hdr=tmp;
  2353.  
  2354.   PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_GFILE, ("lowpc=%ld  hipc=%ld  nbytes= %ld", hdr.low, hdr.high, hdr.nbytes));
  2355.  
  2356.   /* Allocate the total histogram if we haven't yet done so.  */
  2357.  
  2358.   if (!histo) {
  2359.     nhist=(hdr.nbytes-sizeof (struct gm_header))/sizeof (CHUNK);
  2360.     histo=(unsigned CHUNK *)ck_calloc (nhist, sizeof (unsigned CHUNK));
  2361.   }
  2362.  
  2363.   /* Read this file's histogram and merge it into the total one.  */
  2364.  
  2365.   p=(unsigned CHUNK *)ck_malloc (nhist*sizeof (unsigned CHUNK));
  2366.   ck_fread ((void *)p, sizeof (unsigned CHUNK), nhist, fp);
  2367.   for (n=0; n<nhist; n++) {
  2368.     tothist+=p[n];
  2369.     histo[n]+=p[n];
  2370.     if (debug&DB_GFILE) {
  2371.       static ncol;
  2372.  
  2373.       if (n==0) ncol=0;
  2374.       if (histo[n]) {
  2375.     fprintf (stderr, "s[%05d]=%-3d", n, histo[n]);
  2376.     if (ncol++%10==9) fputc ('\n', stderr);
  2377.       }
  2378.     }
  2379.   }
  2380.   free ((void *)p);
  2381.   PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_GFILE, (""));
  2382.  
  2383.   /* We've read in the histogram, now read in the function call data.
  2384.      Loop, reading one call-graph edge from the file
  2385.      and recording the edge in the graph.  */
  2386.  
  2387.   while (fread ((void *)&calltmp, sizeof (calltmp), 1, fp)==1) {
  2388.     struct symlist *tmp;
  2389.     struct mesym *s_fm, *s_to;
  2390.  
  2391.     /* Find the calling and called functions's symbol entries.  */
  2392.  
  2393. #ifndef atarist /* on the St we dont relocate at zero */
  2394.     s_fm=val_to_sym (calltmp.from);
  2395.     s_to=val_to_sym (calltmp.to);
  2396. #else
  2397.     s_fm=val_to_sym (calltmp.from - hdr.low);
  2398.     s_to=val_to_sym (calltmp.to - hdr.low);
  2399. #endif
  2400.  
  2401.     if (!s_fm || !s_to) {
  2402.       PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_GFILE, ("unknown call %#lx to %#lx %d times.", calltmp.from, calltmp.to, calltmp.ncalls));
  2403.       continue;
  2404.     }
  2405.  
  2406.     /* Updated total numbers of calls from this caller and to this callee.  */
  2407.  
  2408.     s_to->ncalled+=calltmp.ncalls;
  2409.     s_fm->ncalls+=calltmp.ncalls;
  2410.  
  2411.     /* Add these calls to the edge between them.  */
  2412.  
  2413.     add_to_lists (s_fm, s_to, calltmp.ncalls);
  2414.  
  2415.     PRINT_OBNOXIOUS_DEBUG_MESSAGE
  2416.       (DB_GFILE, ("call %s (%#lx) to %s (%#lx) %ld times", s_fm->name,
  2417.          calltmp.from, s_to->name, calltmp.to, calltmp.ncalls));
  2418.   }
  2419.  
  2420.   ck_fclose (fp);
  2421. }
  2422.  
  2423. /* Print one of those obnoxious and vaguely informative blurbs that we know
  2424.    and love so well.  Used to be, we'd print them out of a file, but nowaday
  2425.    the blurb is encoded direcly into the program.  Makes printing them
  2426.    much faster.  Also means bozo syswiz can't accidentally delete/move/etc the
  2427.    blurb file on us.  */
  2428.  
  2429. void
  2430. print_blurb FUN1(char *, blurb)
  2431. {
  2432.   if (! no_blurbs)
  2433.     fputs (blurb, stdout);
  2434. }
  2435.  
  2436. #ifdef HAVE_SETITIMER
  2437.  
  2438. get_ticks()
  2439. {
  2440.   struct itimerval tim;
  2441.   
  2442.   tim.it_interval.tv_sec = 0;
  2443.   tim.it_interval.tv_usec = 1;
  2444.   timerclear(&tim.it_value);
  2445.   if (setitimer(ITIMER_REAL, &tim, 0) < 0
  2446.       || setitimer(ITIMER_REAL, 0, &tim) < 0) {
  2447.     fprintf(stderr, "%s: setitimer: ", myname);
  2448.     perror((char *)0);
  2449.     exit(1);
  2450.   }
  2451.   return 1000000/tim.it_interval.tv_usec;
  2452. }
  2453.  
  2454. #else /* not HAVE_SETITIMER */
  2455.  
  2456. /* Find the number of clock ticks/second by reading the kernel's memory.
  2457.    This means that if /dev/kmem isn't readable, this program will have to run
  2458.    set[ug]id to someone who can read the kernel.   setgid is probably best.  */
  2459.  
  2460. long
  2461. get_ticks FUN0()
  2462. {
  2463. #ifndef atarist
  2464.   static struct nlist nl[2];
  2465. #endif
  2466.   long    ret;
  2467.   FILE    *fp;
  2468. #ifdef atarist
  2469.   ret = 50;    /* 200 hz timer / 4, see lib/gmon.c */
  2470. #else
  2471. #ifdef USG
  2472. #include <sys/types.h>
  2473. #include <sys/param.h>
  2474.   ret = HZ;
  2475. #else
  2476.   nl[0].n_un.n_name="_hz";
  2477.   nlist ("/vmunix", &nl[0]);
  2478.   if (nl[0].n_type==0)
  2479.     fatal ("Can't find `%s' in namelist of /vmunix", nl[0].n_un.n_name);
  2480.   fp=ck_fopen ("/dev/kmem", "r");
  2481.   ck_fseek (fp, (long)nl[0].n_value, 0);
  2482.   ck_fread ((void *)&ret, sizeof (ret), 1, fp);
  2483.   fclose (fp);    /* This really should be ck_fclose, but on some systems,
  2484.              closing devices returns -1, errno=NOT_OWNER, which
  2485.            really screws things up. . . */
  2486. #endif
  2487. #endif
  2488.   PRINT_OBNOXIOUS_DEBUG_MESSAGE (DB_MISC, ("get_ticks ()=%ld", ret));
  2489.   return ret;
  2490. }
  2491. #endif /* not HAVE_SETITIMER */
  2492.  
  2493. /* Record one -e, -E, -f or -F option in `filters', and check for conflicts.
  2494.    All these options are recorded there for processing later
  2495.    once the call-graph has been constructed.  */
  2496.  
  2497. void
  2498. add_filter FUN2(char *, name, int, type)
  2499. {
  2500.   if (!filters) {
  2501.     filters=(struct filter *)ck_malloc (sizeof (struct filter));
  2502.     nfilters=1;
  2503.   } else {
  2504.     int    n;
  2505.  
  2506.     for (n=0; n<nfilters; n++) {
  2507.       if (filters[n].name==name || !strcmp (filters[n].name, name))
  2508.     fatal ("Conflicting options for function `%s'", name);
  2509.     }
  2510.     nfilters++;
  2511.     filters=(struct filter *)ck_realloc ((void *)filters, sizeof (struct filter)*nfilters);
  2512.   }
  2513.   filters[nfilters-1].name=name;
  2514.   filters[nfilters-1].type=type;
  2515. }
  2516.  
  2517. /* Data base associating stdio streams with the file names that are open.  */
  2518.  
  2519. struct file_to_name {
  2520.     FILE *stream;    /* A stdio stream */
  2521.     char *name;    /* The file name (malloc'd specially for this list) */
  2522.     struct file_to_name *next;
  2523. };
  2524.  
  2525. struct file_to_name *files_to_names;
  2526.  
  2527. /* Given a stdio stream, look it up in the data base and return
  2528.    the file name.  */
  2529.  
  2530. char *
  2531. stream_name FUN1(FILE *, stream)
  2532. {
  2533.   struct file_to_name *tail;
  2534.  
  2535.   for (tail = files_to_names; tail; tail = tail->next)
  2536.     if (tail->stream == stream)
  2537.       return tail->name;
  2538.  
  2539.   return "unknown file";
  2540. }
  2541.  
  2542. /* Open a file like `fopen', but report a fatal error if it fails;
  2543.    if it succeeds, record the stream and filename in the data base of such.  */
  2544.  
  2545. FILE *
  2546. ck_fopen FUN2(char *, name, char *, mode)
  2547. {
  2548.   FILE *stream;
  2549.   int n;
  2550.   struct file_to_name *new;
  2551.  
  2552.   stream=fopen (name, mode);
  2553.   if (stream==(FILE *)0)
  2554.     fatal_io ("Couldn't open", name);
  2555.  
  2556.   new = ck_malloc (sizeof (struct file_to_name));
  2557.   new->name = ck_malloc (strlen (name) + 1);
  2558.   strcpy (new->name, name);
  2559.   new->stream = stream;
  2560.   new->next = files_to_names;
  2561.   files_to_names = new;
  2562.   return stream;
  2563. }
  2564.  
  2565. /* Interfaces to various functions of stdio
  2566.    which use the stream/filename database to print an error message
  2567.    if the function gets an error.  */
  2568.  
  2569. void
  2570. ck_fseek FUN3(FILE *, stream, long, i, int, w)
  2571. {
  2572.   if (fseek (stream, i, w)==-1)
  2573.     fatal_io ("Couldn't lseek", stream_name (stream));
  2574. }
  2575.  
  2576. void
  2577. ck_fread FUN4(void *, ptr, size_t, size, size_t, nmemb, FILE *, stream)
  2578. {
  2579.   if (fread (ptr, size, nmemb, stream)!=nmemb)
  2580.     fatal_io ("Couldn't read", stream_name (stream));
  2581. }
  2582.  
  2583. void
  2584. ck_fwrite FUN4(void *, ptr, size_t, size, size_t, nmemb, FILE *, stream)
  2585. {
  2586.   if (fwrite (ptr, size, nmemb, stream)!=nmemb)
  2587.     fatal_io ("Couldn't write", stream_name (stream));
  2588. }
  2589.  
  2590. void
  2591. ck_fclose FUN1(FILE *, stream)
  2592. {
  2593.   struct file_to_name *tail;
  2594.  
  2595.   if(stream->_flag & _IOWRT)
  2596.     fflush (stream);
  2597.   if (ferror (stream))
  2598.     fatal ("I/O error on `%s'", stream_name (stream));
  2599.   if (fclose (stream)==EOF)
  2600.     fatal_io ("Couldn't close", stream_name (stream));
  2601.  
  2602.   if (files_to_names && files_to_names->stream == stream)
  2603.     files_to_names = files_to_names->next;
  2604.   else
  2605.     for (tail = files_to_names; tail->next; tail = tail->next)
  2606.       if (tail->next->stream == stream)
  2607.     {
  2608.       struct file_to_name *loser = tail->next;
  2609.       tail->next = tail->next->next;
  2610.       free (loser->name);
  2611.       free (loser);
  2612.       return;
  2613.     }
  2614. }
  2615.  
  2616. /* Report a fatal error doing I/O, and exit.
  2617.    PROBLEM is "Couldn't whatever" and NAME is the file name.  */
  2618.  
  2619. void
  2620. fatal_io FUN2(char *, problem, char *, name)
  2621. {
  2622.   fprintf (stderr, "%s: %s %s:", myname, problem, name);
  2623.   perror (0);
  2624.   exit (101);
  2625. }
  2626.  
  2627. /* Report a fatal error and exit. Arguments like `printf'.  */
  2628.  
  2629. void
  2630. fatal FUN1N(char *, s)
  2631. {
  2632.   va_list iggy;
  2633.  
  2634.   var_start (iggy, s);
  2635.   fprintf (stderr, "%s: ", myname);
  2636.   vfprintf (stderr, s, iggy);
  2637.   /* _doprnt (s, iggy, stderr); */
  2638.   putc ('\n', stderr);
  2639.   va_end (iggy);
  2640.  
  2641.   exit (102);
  2642. }
  2643.  
  2644. /* Memory allocation functions.  */
  2645.  
  2646. /* This function is called just like `printf'
  2647.    except that the output is put into a new string
  2648.    allocated with `malloc'.
  2649.    Returns the address of the string.  The caller must free the string.  */
  2650.  
  2651. char *
  2652. mk_sprintf FUN1N(char *, str)
  2653. {
  2654.   char tmpbuf[2048];
  2655.   char *ret;
  2656.   va_list iggy;
  2657.  
  2658.   var_start (iggy, str);
  2659.   vsprintf (tmpbuf, str, iggy);
  2660.   va_end (iggy);
  2661.  
  2662.   ret=ck_malloc (strlen (tmpbuf)+1);
  2663.   strcpy (ret, tmpbuf);
  2664.   return ret;
  2665. }
  2666.  
  2667. /* Encapsulations of `malloc', `calloc' and `realloc'
  2668.    that cause fatal errors if there is not enough memory.  */
  2669.  
  2670. void *
  2671. ck_malloc FUN1(size_t, size)
  2672. {
  2673.   void *ret;
  2674.   void *malloc ();
  2675.  
  2676.   ret=malloc (size);
  2677.   if (ret==(void *)0)
  2678.     fatal ("Virtual memory exhausted");
  2679.   return ret;
  2680. }
  2681.  
  2682. void *
  2683. ck_calloc FUN2(size_t, nmemb, size_t, size)
  2684. {
  2685.   void *ret;
  2686.   void *calloc ();
  2687.  
  2688.   ret=calloc (nmemb, size);
  2689.   if (ret==(void *)0)
  2690.     fatal ("Virtual memory exhausted");
  2691.   return ret;
  2692. }
  2693.  
  2694. void *
  2695. ck_realloc FUN2(void *, ptr, size_t, size)
  2696. {
  2697.   void *ret;
  2698.   void *realloc ();
  2699.  
  2700.   ret=realloc (ptr, size);
  2701.   if (ret==(void *)0)
  2702.     fatal ("Virtual memory exhausted");
  2703.   return ret;
  2704. }
  2705.  
  2706. /* Implement an expandable fifo buffer of pointers
  2707.    (we don't care what they point to)
  2708.    which is used for conducting breadth-first tree walks in the call graph.
  2709.  
  2710.    The fifo is implemented as a ring buffer.  */
  2711.  
  2712. struct ring_buf
  2713. {
  2714.   void **buf;
  2715.   int size;
  2716.   void **push_to_here;
  2717.   void **pop_frm_here;
  2718. };
  2719.  
  2720. void *
  2721. init_ring_buffer FUN0()
  2722. {
  2723.   struct ring_buf *ret;
  2724.  
  2725.   ret=ck_malloc (sizeof (struct ring_buf));
  2726.   ret->size=40;
  2727.   ret->buf=(void **)ck_calloc (ret->size, sizeof (void **));
  2728.   ret->push_to_here=ret->buf;
  2729.   ret->pop_frm_here=ret->buf;
  2730.   return ret;
  2731. }
  2732.  
  2733. void
  2734. push_ring_buffer FUN2(void *, b, void *, n)
  2735. {
  2736.   struct ring_buf *buf;
  2737.  
  2738.   buf=(struct ring_buf *)b;
  2739.   if (buf->push_to_here+1==buf->pop_frm_here
  2740.       || (buf->pop_frm_here==buf->buf
  2741.       && buf->push_to_here==buf->buf+(buf->size-1))) {
  2742.     int f, t, from_num;
  2743.  
  2744.     f=buf->pop_frm_here-buf->buf;
  2745.     t=buf->push_to_here-buf->buf;
  2746.     from_num=buf->size-f;
  2747.  
  2748.     buf->size*=2;
  2749.     buf->buf=ck_realloc ((void *)buf->buf, buf->size*sizeof (void **));
  2750.     if (t==0) {
  2751.       buf->push_to_here=buf->buf+f+from_num;
  2752.       buf->pop_frm_here=buf->buf+f;
  2753.     } else if (t>f) {
  2754.       buf->push_to_here=buf->buf+t;
  2755.       buf->pop_frm_here=buf->buf+f;
  2756.     } else {
  2757.       buf->push_to_here=buf->buf+t;
  2758.       buf->pop_frm_here=buf->buf+(buf->size-from_num);
  2759.       if (from_num)
  2760.     bcopy (buf->buf+f,
  2761.            buf->pop_frm_here,
  2762.            from_num*sizeof (void **));
  2763.     }
  2764.   }
  2765.   *(buf->push_to_here)=n;
  2766.   buf->push_to_here++;
  2767.   if (buf->push_to_here==buf->buf+buf->size)
  2768.     buf->push_to_here=buf->buf;
  2769. }
  2770.  
  2771. void *
  2772. pop_ring_buffer FUN1(void *, b)
  2773. {
  2774.   struct ring_buf *buf;
  2775.   void *ret;
  2776.  
  2777.   buf=(struct ring_buf *)b;
  2778.   ret= *(buf->pop_frm_here);
  2779.   buf->pop_frm_here++;
  2780.   if (buf->pop_frm_here==buf->buf+buf->size)
  2781.     buf->pop_frm_here=buf->buf;
  2782.   return ret;
  2783. }
  2784.  
  2785. int
  2786. ring_buffer_isnt_empty FUN1(void *, b)
  2787. {
  2788.   struct ring_buf *buf;
  2789.  
  2790.   buf=(struct ring_buf *)b;
  2791.   return buf->pop_frm_here!=buf->push_to_here;
  2792. }
  2793.  
  2794. void
  2795. flush_ring_buffer FUN1(void *, b)
  2796. {
  2797.   struct ring_buf *buf;
  2798.  
  2799.   buf=(struct ring_buf *)b;
  2800.   free (buf->buf);
  2801.   free (buf);
  2802. }
  2803.  
  2804.  
  2805. /* Functions to print debugging messages.  */
  2806.  
  2807. /* Like vprintf but print an extra newline at the end.  */
  2808.  
  2809. void
  2810. dbgprintf FUN1N(char *, s)
  2811. {
  2812.   va_list iggy;
  2813.  
  2814.   var_start (iggy, s);
  2815.   vfprintf (stderr, s, iggy);
  2816.   putc ('\n', stderr);
  2817.   va_end (iggy);
  2818. }
  2819.  
  2820. /* Print out the entire symbol table */
  2821.  
  2822. void
  2823. dumpsyms FUN0()
  2824. {
  2825.   struct mesym *np, *endp;
  2826.   struct symlist *sy;
  2827.   int n;
  2828.   char buf[80];
  2829.  
  2830.   n=0;
  2831.   for (np=syms, endp= &syms[nsym]; np<endp; np++) {
  2832.   char *demangled = cplus_demangle (np->name);
  2833.   char *name;
  2834.  
  2835.     if (demangled)
  2836.       name = demangled;
  2837.     else {
  2838.       name = np->name;
  2839. #ifndef nounderscore
  2840.       if (*name == '_')
  2841.     name++;
  2842. #endif
  2843.     }
  2844.     
  2845.     sprintf (buf, "%-15s %#6lx %d %2d.%2d[%d]",
  2846.          name, np->value, np->histo, np->ncalled, np->ncalls, np->cycnum);
  2847.     if (demangled != NULL)
  2848.       free (demangled);
  2849.     
  2850.     if (n==0) {
  2851.       printf ("%-35s", buf);
  2852.       n++;
  2853.     } else {
  2854.       printf ("%s\n", buf);
  2855.       n=0;
  2856.     }
  2857.   }
  2858.   putchar ('\n');
  2859.   if (n==0) putchar ('\n');
  2860. }
  2861.  
  2862. /* Print out the call tree vector */
  2863. void
  2864. dumpfuns FUN0()
  2865. {
  2866.   struct mesym *np;
  2867.   struct symlist *sy;
  2868.   int    n;
  2869.  
  2870.   for (n=0; n<number_of_functions_in_call_tree; n++) {
  2871.     np=functions_in_call_tree[n];
  2872.     fprint_name (stdout, np->name);
  2873.     printf ("{%d}[%d]  ", np->cycnum, np->ncalls);
  2874.     /* for (sy=np->called; sy; sy=sy->next)
  2875.        printf ("%s{%d}(%d) ", sy->sym->name, sy->sym->cycnum, sy->ncalls);
  2876.        putchar ('\n'); */
  2877.   }
  2878. }
  2879.  
  2880. #ifdef atarist    /* atariSt specific versions */
  2881.  
  2882. /* Return TRUE if the asym-entry SYM describes a symbol
  2883.    that gprof should NOT pay attention to.  */
  2884.  
  2885. #define ISDRIVE(d) ( \
  2886.     (((d) >= 'A') && ((d) <= 'Z')) || \
  2887.         (((d) >= 'a') && ((d) <= 'z'))    )
  2888. int
  2889. badsym FUN1(struct asym *, sym)
  2890. {
  2891.   if(sym->a_type == A_UNDF)
  2892.     return TRUE;  /* these are the symbols ld (write_atari_sym()) had faked */
  2893.   if (!(sym->a_type & A_TEXT))
  2894.     return TRUE;
  2895.   if (no_locals && !(sym->a_type & A_GLOBL))
  2896.     return TRUE;
  2897.   if(!strncmp(sym->n_un.ptr, "gcc_comp", 8L))
  2898.     return TRUE;
  2899.   if(ISDRIVE((sym->n_un.ptr)[0]) && ((sym->n_un.ptr)[1] == ':'))
  2900.    return TRUE;
  2901.   /* Filenames or pascal labels should be ignored */
  2902.   if (index (sym->n_un.ptr, '.') || index (sym->n_un.ptr, '$') ||
  2903.       index (sym->n_un.ptr, '/') || index (sym->n_un.ptr, '\\')  )
  2904.     return TRUE;
  2905.   return FALSE;
  2906. }
  2907.  
  2908. /* Read symbols from a.out file open on FP.  There are N of them.  Allocate a
  2909.    vector to store the interesting ones in, and sort it numerically.  */
  2910.  
  2911. void
  2912. read_syms FUN2(FILE *, fp, int, n)
  2913. {
  2914.   struct asym *tmpsyms;
  2915.   char *s, *p, *q;
  2916.   int i,j,is_long;
  2917.   
  2918.   /* creat space for incore name strings */
  2919.   s = strs = ck_malloc(n*25); /* slight overkill */
  2920.  
  2921.   /* Read the entire symbol table.  */
  2922.   tmpsyms=(struct asym *)ck_malloc (n*sizeof (struct asym));
  2923.   ck_fread (tmpsyms, sizeof (struct asym), n, fp);
  2924.  
  2925.    
  2926.   /* Count the useful symbols.
  2927.      Also relocate their name-fields to be C string pointers.  */
  2928.   for(nsym = 0, i = 0; i < n; i++)
  2929.   {
  2930.       for(j = 0, p = &(tmpsyms[i].n_un.a_name[0]), q = s;
  2931.       (j < 8) && (*p != '\0'); j++) 
  2932.       *s++ = *p++;
  2933.       if ((tmpsyms[i].a_type & A_LNAM) == A_LNAM)
  2934.     {
  2935.       max_atari_sym_length = 22;
  2936.       is_long = TRUE;
  2937.       if (i + 1 < n)
  2938.         for (j = 0, p = &tmpsyms[i+1].n_un.a_name[0];
  2939.          j < 14 && *p != '\0'; j++)
  2940.           *s++ = *p++;
  2941.       else
  2942.         /* there's something wrong; assume short symbol */
  2943.         is_long = FALSE;
  2944.     }
  2945.       else
  2946.     is_long = FALSE;
  2947.       *s++ = '\0';
  2948.       tmpsyms[i].n_un.ptr = q;
  2949.       if (badsym (&tmpsyms[i]))
  2950.       {
  2951.       tmpsyms[i].n_un.ptr = (char *)0;
  2952.       }
  2953.       else
  2954.       {
  2955.       nsym++;
  2956.       }
  2957.       if (is_long)
  2958.     /* skip symbol name extension */
  2959.     tmpsyms[++i].n_un.ptr = NULL;
  2960.   }
  2961.   
  2962.   /* Allocate permanent space and copy useful symbols into it.  */
  2963.   syms=(struct mesym *)ck_calloc (nsym, sizeof (struct mesym));
  2964.  
  2965.   for (i=0, j = 0; i < n; i++) {
  2966.     if (tmpsyms[i].n_un.ptr != (char *)0) {
  2967.       syms[j].name = tmpsyms[i].n_un.ptr;
  2968. /* #ifndef nounderscore */
  2969. #if 0
  2970.       /* Remove the initial _ from the symbol name */
  2971.       if (*(syms[j].name)=='_')
  2972.     syms[j].name++;
  2973. #endif
  2974.       syms[j].value = tmpsyms[i].a_value;
  2975.       j++;
  2976.     }
  2977.   }
  2978.  
  2979.   if (j != nsym)
  2980.     exit (99);
  2981.  
  2982.   free ((void *)tmpsyms);
  2983.  
  2984.   /* Put symbols in numeric order.  */
  2985.   qsort (syms, nsym, sizeof (struct mesym), symcmp);
  2986. }
  2987. #endif /* atarist */
  2988.  
  2989. void print_version FUN0()
  2990. {
  2991. #ifdef atarist
  2992. #  include "PatchLev.h"
  2993.     fprintf(stderr, "GNU gprof for atariST-TOS Patchlevel %s (%s)\n",
  2994.     PatchLevel, __DATE__);
  2995. #else
  2996.     fprintf(stderr, "GNU gprof\n");
  2997. #endif
  2998. }
  2999.  
  3000. /* JF someone put this stuff in the file before all the prototyes et all.
  3001.    I moved them here where they belong */
  3002.  
  3003. /* Like malloc but abort if out of memory.  */
  3004. void *
  3005. xmalloc FUN1(size_t, size)
  3006. {
  3007.     return ck_malloc(size);
  3008. }
  3009.  
  3010. /* Like realloc but abort if out of memory.  */
  3011. void *
  3012. xrealloc FUN2(void *, p, size_t, size)
  3013. {
  3014.   return ck_realloc(p,size);
  3015. }
  3016.  
  3017. /* Print NAME on STREAM, demangling if necessary.  */
  3018. void
  3019. fprint_name FUN2(FILE *, stream, char *, name)
  3020. {
  3021.   char *demangled = cplus_demangle (name);
  3022.   if (demangled == NULL)
  3023.   {
  3024. #ifndef nounderscore
  3025.       if (*name == '_')
  3026.     name++;
  3027. #endif
  3028.       fputs (name, stream);
  3029.   }
  3030.   else
  3031.     {
  3032.       fputs (demangled, stream);
  3033.       free (demangled);
  3034.     }
  3035. }
  3036.  
  3037. #if 0
  3038. /* let this be dummy for now, 
  3039.    if you want to throw in the cplusplus name demangler, simple
  3040.    delete this function, and link with cplus-dem.o
  3041. */
  3042. char *cplus_demangle FUN1(char *, name)
  3043. {
  3044.   return NULL;
  3045. }
  3046. #endif
  3047.